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.
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:
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:
By providing 0 0/5 * * * *, the function will run every 5 minutes from the first run.
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.
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.
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.
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:
Once the initial setup is done, test the API.
In the next section, we will discuss 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:
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:
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.
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