In this article I will show you how to create a timer triggered function. The first function triggers every 10 seconds and writes the message to the Azure queue, so the out binding for this function here is the Azure queue. In the second function we will be setting the input binding as Azure queue, on receiving a message in Azure queue the second function will trigger and set the out binding as Azure table, which writes the data as output to the Azure table.
What is function binding?
Function binding is a way to define input and output for a function. This configuration information stays in function.json file.
To complete this tutorial, you'll need the following,
- An active Azure account. (If you don't have an account, you can create a free trial account in just a couple of minutes. For details, see Azure Free Trial.)
Please follow the steps, given below, to create Azure function binding with Azure queue and Azure table in the preview portal.
- Login to Azure Portal.
- Create the Azure function setup as explained in below article,
- In the function app blade as shown in the below image, perform the below steps:
- Select TimerTriggered-C# template: This configures the function to auto trigger on a specified time.
- Name the function in Name in your function text box.
- Enter the timer setting in the expression as I did, like 0/10 * * * * *
- Click on Create button.
Note: In Configure there is a Timer triggered (myTimer), so here myTimer in braces is a timer object passed to function, every time the function is invoked.
The above step will create the Azure function which will be auto triggered on a given schedule in Schedule text box, as shown in the below image.
In function signature you will find the object myTimer1 as TimerInfo object. The name is slightly changed as seen from step 3 as I have changed it.
In the Logs window you will find all the information related to the running of the function.
As logs show, the function started and is auto triggering at interval of 10 seconds and completing successfully.
Now we will set the out binding as Azure queue for this function, where function will write the messages.
- Go to Integrate tab and configure the settings as shown in the below image,
- Click on the + New Output to set the out binding for selected function. Currently there is only one function as Demo-TimerTrigger-Function, however there are multiple functions, then make sure for which function we are making changes.
- Select Azure Storage Queue.
- Click on Select button.
The above action will result in the below result as shown in the image.
Perform the below steps in this window.
- Give a message parameter name in the Message parameter name text box, which is used to carry our message and to write to the queue as we see in the coming steps.
- Select Storage account by clicking on select link as shown in below image.
- Give the queue a name in the Queue name text box; you can give the existing queue name if queue is already exists, otherwise a queue will be created so make sure you will provide a valid queue name which is allowed by Azure.
- Click on Save button after setting all information successfully, as the Save button enables after setting up valid storage account as shown in the below image.
The above step's final action will take you to the Develop tab as shown in below image.
In Develop tab in Code window I have added an out parameter named as outputQueueItem which we have given in the above step while setting up a storage account for output binding outputQueueItem is a type of MyQueueItem which is defined as a poco class with auto properties Message and Time of type string.
We added the code of constructing MyQueueItem object and assigning it to outputQueueItem as in function body as shown in the above image.
After doing all the code changes, click on Save button and observe the Logs, where we will get the function compilation started, trigger and completed status information, and all other information if function fails.
After successful compilation and running of the function start writing messages to the Azure queue. As we can check this by using some third party tools for Azure storage or Azure storage explorer by Microsoft.
I am checking this using the visual studio cloud explorer. Follow the below steps for this.
- Start visual studio as administrator.
- As shown in the below image from View menu click on Cloud Explorer.
The above step will open the cloud explorer, if you have not signed into visual studio with credentials you are using for Azure subscription then it will ask you for login, after login you can filter your resource by resource group or by resource type and also can filter your subscription as I have filtered my resource by resource group and subscription.
Below image shows the messages written to the Azure queue.
In the cloud Explorer select resource group, expand it, select storage used for out binding for Function in step 4. Expand the Queue and select the queue to which we have given the name (outqueue) in step 4. As the queue was not created earlier, it’s created by the azure function.
Now we will see the second part of this article for setting the A queue as a trigger and Azure table as out binding for function.
- In the Function app blade create a new function as shown in the below image.
- Click on + New Function.
- Select the QueueTrigger-C# template.
- Give function name in Name your function text box as I have given Demo-QueueTrigger-Function.
- Select storage account by clicking select.
- Give the queue name which a function will listen to in order to receive the message
Note: Here we are using the same storage account and queue we used in first function.
- Click on Create button.
The above step creates a Demo-QueueTrigger-Function function and starts triggering as there are already messages in outqueue which the function Demo-QueueTrigger-Function is listening to, as you can see the newly created function and function running statuses in Logs window as shown in the below image.
A first parameter to the function is a string myQueueItem which takes the json string as we can verify it from the Logs window where we are showing the message as highlighted.
At this time if you checked the outqueue queue, as shown in the below image, all the messages would be dequeued, as the queue is an input binding for Demo-QueueTrigger-Function, hence functions has retrieved all the messages from function; now, when a new message will arrive in the queue, the function will trigger.
Now we have to assign Azure table as the out binding for function Demo-QueueTrigger-Function.
- In Function app blade follow the steps as shown in the below image.
- Select function Demo-QueueTrigger-Function.
- Click on Integrate tab.
- Click on + New Output.
- Select Azure Storage Table.
- Click Select.
The above action will result into the below shown result.
- Provide the table parameter name in Table parameter name text box, this will be the parameter name we will use in the function to write record to Azure table.
- Select storage account.
- Give table name in Table name text box , if table is not created already then Azure function will create for us, so provide a valid function name allowed by azure.
- Click on Save button.
Now the configuration for out binding is done, now go to Develop tab for function Demo-QueueTrigger-Function and perform the changes to function code as shown in the below image.
Below are the changes done in original function code.
- Added two poco classes as QueueItem and TableItem
- In function signature added replaced string type with QueueItem.
Note: Azure function takes care of serialization and deserialization.
- In function signature added a parameter of ouputTable.
Note: Here ICollector is an interface of Azure web job sdk, as Azure functions uses azure web job sdk internally.
- Added the code to construct TableItem and added the result to outputTable parameter which internally inserts the record in the Azure table.
After doing the above changes Save the function. And observe the Logs window for function statuses. From visual studio you can see the records as we have seen for queue written to azure table as shown in the below image.
As we have seen, it's easy to configure the bindings for Azure functions for Azure queue and Azure table. Azure function provides lots of templates to configure other Azure services as binding for Azure functions.