8 min read

A trigger is an event or situation that causes something to start. This something can be some sort of processing of data or some other service that performs some action. Triggers are just a set of functions that get executed when some event gets fired.

In Azure, we have different types of triggers, such as an implicit trigger, and we can also create a manual trigger. With, Azure Functions you can write code in response to a trigger in Azure.

[box type=”shadow” align=”” class=”” width=””]This article is taken from the book Learning Azure Functions by Manisha Yadav and Mitesh Soni. In this book, you will you learn the techniques of scaling your Azure functions and making the most of serverless architecture. [/box]

In this article, we will see the common types of triggers and learn how to create a trigger with a very simple example. We will also learn about the HTTP trigger, event bus, and service bus.

Common types of triggers

Let’s understand first how a trigger works and get acquainted with the different types of triggers available in Azure Functions.

The architecture of a trigger and how it works is shown in the following figure:

The preceding diagram shows the event that fires the trigger and once the trigger is fired, it runs the Azure Function associated with it. We need to note a very important point here: one function must have exactly one trigger; in other words, one function can’t have multiple triggers.

Now let’s see the different types of trigger available in Azure:

  • TimerTrigger: This trigger is called on a predefined schedule. We can set the time for execution of the Azure Function using this trigger.
  • BlobTrigger: This trigger will get fired when a new or updated blob is detected. The blob contents are provided as input to the function.
  • EventHubTrigger: This trigger is used for the application instrumentation, the user experience, workflow processing, and in the Internet of Things (IoT). This trigger will get fired when any events are delivered to an Azure event hub.
  • HTTPTrigger: This trigger gets fired when the HTTP request comes.
  • QueueTrigger: 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 have to display a “good morning” message on screen every day in the morning at 8 AM. This situation is related to time so we need to use a schedule trigger. We will look at this type of trigger later in this article. Let’s first start creating a function with the schedule trigger first:

  1. Log in to the Azure Portal.
  2. Click on the top left + icon | Compute | Function App:

  1. Once we click on Function App, the next screen will appear, where we have to provide a unique function App name, Subscription, Resource Group, Hosting Plan, Location, Storage, and then click on the Create button:

  1. Once we click on the Create button, Azure will start to deploy this function. Once this function is deployed, it will be seen in Notifications, as shown in the following screenshot:

  1. Click on Notifications and check the Functions details and add the trigger:

  1. To add a trigger in this function, click on the + icon next to Functions and then click on Custom function:

  1. Now we have to select Language and type the name of the trigger. Once we provide the name and trigger value it will provide the available template for us after filtering all the templates:

  1. 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.

  1. Once we click on the Create button, we will see the template code on the screen as follows:

Here, we have to write code. Whatever action we want to perform, we have to write it here. Now write the code and click on the Save and run button.

  1. Once we run the code, we can see the output in the logs, as shown in the following screenshot:

Note the timing. It runs at an interval of 5 minutes. Now we want it to run only once a day at 8 AM. To do this, we have to change the value of the schedule.

  1. To edit the value in the trigger, click on Integrate, type the value, and then click on the Save button:

  1. Now, again, click on goodMorningTriggerJS, modify the code, and test it.

So, this is all about creating a simple trigger with the Azure Function. Now, we will look at the different types of triggers available in Azure.

HTTP trigger

The HTTP trigger is normally used to create the API or services, where we request for data using the HTTP protocol and get the response. We can also integrate the HTTP trigger with a Webhook.

Let’s start creating the HTTP trigger. We have already created a simple Azure Function and trigger. Now we will create the HTTP Login API. In this, we will send the login credential through an HTTP post request and get the response as to whether the user is valid or not.

Since we have already created a Function app in the previous example, we can now add multiple functions to it:

  1. Click on +, select HttpTrigger-JavaScript, provide the function name, and click on the Create button:

  1. After we click on the Create button, the default template will be available. Now, we can edit and test the function:

  1. Now edit the code as follows:

  1. Save and run the code, as shown in the following screenshot:

  1. The login service is ready. Now let’s check this service in Postman.
  2. To get the URL from the function, click on Get function URL:

  1. We will use Postman to check our service. Postman is a Chrome extension for API developers to test APIs. To add the Chrome extension, go to Settings in Chrome and select More tools | Extensions, as shown in the following screenshot:

  1. Click on Get more extensions:

  1. Now search for postman and then click on + ADD TO CHROME:

  1. Click on Add app:

  1. Launch the Postman app. Click on Sign Up with Google:

  1. The Postman window will look like this:

Once the initial setup is done, test the API.

  1. Copy the function URL and paste it in Postman. Select the method type POST and provide a request body and click on the Send button:

  1. If we provide the correct username and password in the request body, we will get the response, user is valid; otherwise, the response will be invalid user.

In the next section, we will discuss event hubs.

Event hubs

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 a large amount of 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:

Service bus

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 very large numbers of recipients.

Finally, we have completed the trigger part of the Azure Function.

In this article, we have discussed the architecture of a trigger and how the trigger works. We have covered different types of triggers available in Azure. We created one simple example of a schedule trigger and discussed the workflow of the schedule trigger. We discussed the HTTP trigger in detail and how the HTTP trigger works. Then we created an API using the HTTP trigger. We covered the event hub, service bus. We also covered how a trigger works with these services.

If you found this post useful, do check out the book, Learning Azure Functions. This book walks you through the techniques of scaling your Azure functions and will help you make the most of serverless architecture.

Read Next

Anatomy of an Azure function App [Tutorial]

Implementing Identity Security in Microsoft Azure [Tutorial]

Azure Functions 2.0 launches with better workload support for serverless