You are currently viewing Azure Functions With Triggers and Bindings

Azure Functions With Triggers and Bindings

In this article, we will try to create an Azure Function which is triggered whenever there is a message on the queue. We will also try to understand how to add bindings to the functions.

For following some of the steps in this article, you will need an Azure account. If you do not have an Azure account,  you can create free account on Azure.

Let’s have closer look at Bindings

Azure Function App can contain many different small pieces of code or Functions. Every function has exactly one Trigger and optionally it also may have Bindings.

A trigger is something which invokes the function. The trigger may be an HTTP request to a URL, or it may be a timer. There are many triggers available for Azure Functions. Every function should have only one trigger. Trigger can also have data associated with it. For ex. HTTP trigger will give you access to query string parameters or POST parameters.

A binding is something which allows Azure Function to integrate with other systems. Bindings has direction, it may be either input or output.

Well, these are bookish definitions and if you are new to Azure Functions, I am sure you might not have clear idea just by reading these definitions.

Let’s consider a couple of scenarios to understand what is Trigger, What is Input Binding and what is output binding in that scenario.

Scenario 1

Azure Function is triggered after every 5 minutes. The function reads the image from Blob and contents of the from CosmosDB and sends email to intended recipients.

  • Here trigger is Timer.
  • The function will have input binding to read data from Blob and CosmosDB.
  • The function will also have output binding to send the email

Scenario 2

Azure function gets triggered when a HTTP Post request is made. The function uses Microsoft Graph API to update an Excel document.

  • Here trigger is HTTP request
  • There is no input binding
  • The output binding is Microsoft Grap API which is writing contents to Excel.

Demo

Let’s try to create a Function which uses Trigger, Input as well as Output bindings.

We will try to create a Function which is triggered whenever an HTTP request is made. So the Azure Function will use HTTP Request as trigger.

The Http Request will have {name} parameter in the route, which will be used in input binding with Blob Storage. The Blob Storage will return the contents of Blob (file) which has same name as the value of this parameter and from a container with name “incontainer”.

The HTTP Request will return the contents of blob in the response.

The job will also have Output Binding to write the contents of blob on storage queue of name “outqueue”

This is very hypothetical scenario but this should provide enough idea on how the triggers and bindings work.

Step 1: Create Storage Account

Creation of storage account is already explained in one of my previous blog article. Alternatively you can also choose to use the storage account which was created because of creation of Function App.

Please note that you should select appropriate storage account while creating bindings.

Step 2: Create a Function under Function App

Navigate to your Function App and click on (+) button to add new Function. Select HTTP triggered function template, provide name and click on create.

Once this is created go to Integrate tab and then add {name} in the route template. This template will help to map the Blob storage file using Binding Expression.

Step 3: Set Input Binding

Navigate to function and under Integrate, click on “New Input” button to add a new input binding. Select Azure Queue Storage and click on Select button will bring you to next panel.

Here you can name the parameter and select which storage account it should connect to. Select the storage account you created in step 1. Then click on Save button.

Step 4: Set Output Binding

Navigate to function and under Integrate, click on “New Output” button to add a new input binding. Select Azure Queue Storage and click on Select button will bring you to next panel.

Please make sure you have selected storage account you created in step 1. Keep the rest of the inputs as default in below panel and click on Save button.

Step 5: Copy Below Code in Your Function

Add below code in the CSX file of your Azure Function. The file will be visible when you click on httpTriggerFunction.

Below code just reads the name parameter value and assigns the input contents to output queue.

#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage;

public static IActionResult Run(HttpRequest req, string inputBlob, out string outputQueueItem, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string name = req.Query["name"];

    outputQueueItem = inputBlob;
    return name != null
            ? (ActionResult)new OkObjectResult($"Hello, {name} and Message is {inputBlob}")
            : new BadRequestObjectResult("Please pass a name on the query string or in the request body");;
}

In order to execute it, create the in-container and add some files there using storage explorer.

Then you can try hitting the http URL with name parameter value equal to name of one of the file. You can see the contents of file on response page. The contents also can be found in storage queue.

You can use below URL to access the function:

https://your-function-app-name.azurewebsites.net/api/filenameInBlob.txt?code=somecode&name=yourname

I hope this provides you sufficient idea of what triggers and bindings are. Please do comment and let me know if you know your thoughts.

Leave a ReplyCancel reply