In this article, we are going to learn how to trigger Function Apps from Queue Storage in Azure. Function Apps has been one of the most popular cloud services of Microsoft Azure. Function Apps allow users to write code in any language and then execute the code in the cloud. There is no infrastructure to be managed and hence is very flexible for writing and building applications on the go. Every Function App can be triggered in multiple ways, for example, by calling the function URL using an HTTP endpoint or from some other functions in Azure. In this article, we are going to trigger the Function App from Queue Storage in Azure and see how to pass a message from the queue to the Function App.
Queue Storage in Azure is another service in Azure that allows users to store multiple messages in it. Users can use a queue to create a list of items that need to be processed one by one. Messages to Queue Storage in Azure can be added by using the HTTP or HTTPS endpoints. Usually, a queue can store data up to 64 KB in size. We can add millions of messages in a queue if it is supported by the storage account.
Pre-requisites
In order to start with this article, it is required that you have a valid Azure Account and a storage account associated with it. If you do not have a storage account, you can still go ahead and create one before proceeding further with this article. You can read about creating a storage account from here.
Creating the Function App
Navigate to the Azure portal and search for Function App on the search bar. The new Function page will appear on the screen. Click on Create to create and configure a new function. The Create Function App blade appears on the right. Multiple tabs need to be configured here. Let us first begin with the Basics tab.
Figure 1 – Configuring the Azure Function App
As you can see in the figure above, you can use the following details to configure your Azure Function App:
- Subscription – This is the name of the subscription under which the Function App is to be created. Usually, it is used for billing purposes
- Resource Group – Resource groups are used to logically group resources that belong to a particular application. It can be a collection of multiple services that perform to achieve the same goal
- Function App name – The name of the Function App that you want to create. You can choose any logical name for the function here
- Publish – This option allows you to choose how to publish your application. You can either choose as code or as a Docker container
- Runtime stack – This is one of the most important options to choose from. Select a suitable runtime that you are familiar with. You can choose from a multiple range of runtimes, however, for this tutorial, I am going to choose .NET
- Version – The latest version of the runtime that you have select. For .NET, it is 3.1.
- Region – This is the region where you are going to create your Function App. Ideally, you should choose this region closer to where you or your users would be
Once these details are configured, move on to the next tab by clicking Next: Hosting.
Figure 2 – Configuring the Function App
In this tab, you can configure the following details.
- Storage account – This is the storage account that you are going to associate with the Function App. You can leave the name as default or you can add up your own storage account names. It should be anything between 3 to 24 characters with numbers and lower-case alphabets only
- Operating system – Since we have selected .NET as our runtime stack, by default, the operating system will be selected as Windows
- Plan – This is the hosting plan that defines how your Function App is going to be billed. I’m going to choose the Serverless mode, as in this plan we pay only for the times for which the Function App runs
Once these details are configured, you can proceed to the next tab by clicking on Next: Monitoring. In this tab, we are going to select No for Application Insights just to keep things simpler. Finally, click on Review + Create to review and create the Function App as per the configurations. The deployment of the Function App might take some time. Once the app is deployed, you can click Go to resource and view it.
Figure 3 – Function App deployed
Creating the Queue Triggered Function
Once the Function App has been created and deployed, the next step is to create the Queue Triggered Function under the function app. Click on Functions on the left pane, and then click on Add to create a new function. The Add Function blade appears.
Figure 4 – Add Queue Triggered Function
Select Azure Queue Storage trigger as the template and provide the following details:
- New Function – The name of the new trigger function that you are going to create
- Queue name – The name of the queue that will be used to trigger this function
- Storage account connection – In this step, you can either use the storage account used by the Function App or create a new one
Figure 5 – Creating the Triggered Function
Click on Add once done. The function will be created.
Creating the Queue
Now that the function has been created, let us go ahead and create the queue from which the function can be triggered. Navigate to the Resource Group in which your Function App has been created. Once on the Resource Group page, search for the Storage Account that has been created for the Function App.
Figure 6 – Resource Group for the Function App
Open the Storage Account and click on Queues on the left pane and click on + Queue to create a new queue.
Figure 7 – Creating the Queue
Provide the Queue Name as the same as provided while creating the Function in the previous step. Click OK once done. This will create the queue and make it ready for use. You can trigger the function by posting a message to the queue.
Triggering the Function
Since the queue is now up and running, we can test it by posting a message to the queue. Open the queue that has been created and click on Add message.
Figure 8 – Adding messages to the queue
Add a message to the queue that you can be consumed by the Function. Click OK once done.
Figure 9 – Queue Function triggered
As you can see in the figure above, the function has been triggered by the queue. As soon as the message was posted, the function has been executed and the results are printed on the console. This is a very simple example of consuming messages from a queue. You can now enhance the function code and apply some processing logic to perform operations as per your needs.
Conclusion
In this article, we have seen how to trigger a Function App using Queue Storage in Azure. This is one of the ways to trigger a Function App. Apart from this, we can also trigger a Function App by making an HTTP request to the Function App endpoint. The Queue Storage in Azure provides a queue to which we can add millions of messages. These messages can be processed by a Function App, one by one. In real life, there are a lot of use cases wherein Function Apps are triggered from Queue Storage in Azure and this is how event-driven architectures are designed. To learn more about event-driven architecture, you can follow the official documentation from Microsoft.
- Getting started with PostgreSQL on Docker - August 12, 2022
- Getting started with Spatial Data in PostgreSQL - January 13, 2022
- An overview of Power BI Incremental Refresh - December 6, 2021