Azure Function Triggers and Bindings | Serverless360
← Return To Home

Azure Function Triggers and Bindings

Introduction

Azure Functions is a serverless compute service that lets you run event-triggered code without having to explicitly provision or manage infrastructure, in other words, Microsoft’s Azure Functions is a modern serverless architecture, offering event-driven cloud computing that is easy for developers to use. This blog is about Azure Functions triggers and bindings.

Azure Function Triggers

Triggers are what cause a function to run. A trigger defines how a function is invoked and a function must have exactly one trigger. Triggers have associated data, which is often provided as the payload of the function.

Types of Triggers

Azure Functions Triggers Types

Now let’s see some of the most common types of triggers available in Azure:

Timer Trigger

This trigger is called on a predefined schedule. We can set the time for execution of the Azure Function using this trigger.

Blob Trigger

This trigger will get fired when a new or updated blob is detected. The blob contents are passed on as input to the function.

Event Hub Trigger

This trigger is used for the application instrumentation, the user experience, workflow processing, and the Internet of Things ( IoT). This trigger will get fired when any events are delivered to an Azure Event Hub.

HTTP Trigger

This trigger gets fired when the HTTP request comes.

Queue Trigger

This trigger gets fired when any new messages come in an Azure Storage Queue.

Generic Webhook

This trigger gets fired when the Webhook HTTP requests come from any service that supports Webhooks.

GitHub Webhook

This trigger is fired when an event occurs in your GitHub repositories. The GitHub repository supports events such as Branch created, delete branch, issue comment, and Commit comment.

Service Bus Trigger

This trigger is fired when a new message comes from a service bus queue or topic.

Example of Creating a Simple Scheduled Trigger in Azure

Consider a simple example where we must display a “good morning” message on the screen every day at 8 AM. This situation is related to time, so we need to use a schedule trigger. Let’s start creating a function with the schedule trigger:

  1. Log in to the Azure Portal
  2. Click on the top left + icon | Compute | Function App Simple Scheduled Trigger Simple Scheduled Trigger
  3. Once we click on Function App, the next screen will appear, where we must provide a unique Function App name, Subscription, Resource Group, Hosting Plan, Location, Storage, and then click on the Create button
  4. Once we click on the Create button, Azure will start deploying this function. Once this function is deployed, it will be seen in the notifications, as shown in the following screenshot Azure Functions Triggers
  5. Click on Notifications and check the Functions details and add the trigger: Azure Functions Triggers
  6. To add a trigger in this function, click on the + icon next to Functions and then click on In-Portal to code in the Azure portal itself
  7. Now we must select the desired template from the available templates: Azure Function Triggers
  8. Scroll down and type the trigger name and schedule. The Schedule value is a six-field CRON expression. Click on the Create button:

    By providing 0 0/5 * * * *, the function will run every 5 minutes from the first run.

    Azure Functions Triggers
  9. Once we click on the Create button, we will see the template code on the screen where we can code. Whatever action we want to perform, we must write it here. Now write the code and click on the Save and Run button

Event Hub Trigger

Event hubs are created to help us with the challenge of handling a huge amount of event-based messaging. The idea is that if we have apps or devices that publish many events in a very short duration (for example, a real-time voting system), then event hubs can be the place where we can send the event.

Event Hubs will create a stream of all the events which can be processed at some point in different ways. An event hub trigger is used to respond to an event sent to an event hub event stream.

The following diagram shows how a trigger works with an Event Hub:

Azure Event Hub Trigger

Service bus Trigger

The service bus is used to provide interaction between services or applications run in the cloud with other services or applications. The service bus trigger is used to give the response to messages which come from the service bus queue or topic.

We have two types of service bus triggers:

  • Service bus queue trigger: A queue is basically for first-in-first-out messages. When a message comes from the service bus, the service bus queue trigger gets fired and the Azure Function is called. In the Azure Function, we can process the message and then deliver it.
  • Service bus topic trigger: The topic is useful for scaling to numbers of recipients.

Azure Function Bindings

A binding is a connection to data within your function. Bindings are optional and come in the form of input and output bindings. An input binding is the data that your function receives. An output binding is the data that your function sends.

Unlike a trigger, a function can have multiple input and output bindings.

Binding to a function is a way of declaratively connecting another resource to the function; bindings may be connected as input bindingsoutput bindings, or both. Data from bindings is provided to the function as parameters. You can mix and match different bindings to suit your needs. Bindings are optional and a function might have one or multiple input and/or output bindings.

Triggers and bindings let you avoid hardcoding access to other services. Your function receives data (for example, the content of a queue message) in function parameters. You send data (for example, to create a queue message) by using the return value of the function.

In .NET, the parameter type defines the data type for input data. For instance, use string to bind to the text of a queue trigger, a byte array to read as binary and a custom type to de-serialize to an object.

Free download this blog as a PDF document for offline read.

Binding direction

All triggers and bindings have a direction property in the function.json file:

  • For triggers, the direction is always in
  • Input and output bindings use inand out
  • Some bindings support a special direction in-out. If you use in-out, only the Advanced editoris available via the Integrate tab in the portal.

When you use attributes in a class library to configure triggers and bindings, the direction is provided in an attribute constructor or inferred from the parameter type.

Supported Bindings

This table shows the bindings that are supported in the major versions of the Azure Functions runtime:

Supported Bindings

In C# and other .NET languages, you can use an imperative binding pattern, as opposed to the declarative bindings in function.json and attributes. Imperative binding is useful when binding parameters need to be computed at runtime rather than design time.

Azure Functions management with Serverless360

As we know the Azure Function App contains any number of functions, Serverless360 has the capability to view their properties including their binding details.

Azure Functions management with Serverless360

Invocation Logs

Accessing the Invocation Logs of the Functions is a significant capability of Serverless360 with respect to Azure Function App management. One shall get a quick summary of the Function invocations based on the success or failure of the invocation. Filtering the invocation logs by date is also possible to view the invocations on a specific day.

Detect Azure Functions failure in Serverless360

Detect Failure in Azure Function Invocations

Watch monitor in Serverless360 can help detect the occurrence of a specific errors in the selected Azure Functions of an Azure Function App. This monitor can inform the stakeholders on the error within 5 minutes of the occurrence of the error with all necessary details required to restore the business.

Read more about Serverless360 Watch monitors here.

Serverless360 helps to manage Azure functions and view their properties including binding details.

Conclusion

Selecting appropriate triggers, input and output bindings for your Azure Function is indeed important to get the function definition right. This blog has covered all the basics of Azure Function triggers and bindings to get started.

Author: Priya Latha

Priya Latha is a Jr.Software Engineer at Kovai.co. She loves working with Angular, . Net and on Serverless technologies. She believes in the quote that "Expect nothing less than perfect".

One thought on “Azure Function Triggers and Bindings”

Comments are closed.