Updated March 16, 2023
Introduction to Azure Function Triggers
Azure function trigger contains the part of the function stack that tells Azure how to invoke our function. Timer, blob, and http are the most commonly used functions in Azure function triggers. Azure function triggers is a serverless compute service used to run event-triggered code without explicitly provisioning infrastructure. The Azure function triggered specifies how the function is invoked.
Key Takeaways
- The azure function trigger defines when a function is invoked and when it is triggered. At the time of creating the trigger function, we define the trigger time.
- There are multiple types of triggers available in the azure function i.e. timer, blob, event, HTTP, and queue trigger.
What are Azure Function Triggers?
The azure function http triggers inform azure that the function will be invoked through an http request. The most recent transactions into the banking applications are obtained via the get request. The binding does not co-exist without defining the function triggers in azure. Triggers define how a function is called; each function contains one trigger.
Basically, an azure function trigger is a mechanism that allows us to manually or automatically invoke a function. As we are considering the trigger as an event, when the event occurs, it will invoke the function using appropriate data, ensuring that the azure function has enough context. There are multiple triggers supported by azure functions like http and many more.
How to Use Azure Function Triggers and Binding?
Below steps shows how we can use the azure function triggers and binding as follows:
1. In the first step, we first login in the Azure portal.
2. After login into the azure portal, click Create a resource icon; after opening the resources tab, we need to select the function app as follows.
3. After selecting the function app, the next window will appear where we need to create the function app as follows.
4. When you click the Create button, Azure starts to create the function; once completed, Azure displays the deployment successful notifications.
5. We click on the function after creating it to see all of its details and to add a new trigger to it.
6. After defining the trigger tab, now in this step, we need to select the desired template from the available templates as follows.
7. After clicking on the specified azure function trigger, now, in this step, we are scheduling the trigger. We need to provide a schedule of triggers in cron format. In the below example, we are creating a trigger that runs every 5 minutes as follows.
Types of Azure Functions Trigger
Below are the types of triggers available in azure. We need to select a specified trigger at the time of creating the function in azure. We are selecting trigger as per the function which we have created.
1. Time Trigger
This trigger is called by using the predefined schedule which we have defined. We can set the time execution for the azure function by using a specified trigger.
Below figure shows the time trigger as follows:
2. Blob Trigger
This trigger is executed when it finds the new updated blob. The content blob is passed as an input function. Below figure shows the blob trigger as follows:
3. Event Hub Trigger
This trigger is used in application instrumentation. This trigger is fired when any event is delivered to the specified azure event. The below figure shows the event hub trigger as follows:
4. Http Trigger
This trigger is fired when the http request comes. Below figure shows the http trigger as follows:
5. Queue
This trigger is executed when a new message comes into the queue of azure storage. The below figure shows the queue trigger as follows:
6. Service Bus Trigger
This trigger is divided into two types. This trigger is fired when a new message comes in a bus query. Below figure shows the service bus trigger as follows:
HTTP Triggers
The http trigger invokes the function by using the http request. We are using http triggers in the azure function for building the serverless APIs and responding to the webhooks.
Below is the default trigger value of the trigger function as follows:
- Http 204 no content – It will define the empty body in function 2.x and higher version.
- Http 200 ok – It will define the empty body in a function of 1.x.
For modifying the http response, we need to configure the output binding. The below example shows trigger binding by using the httptrigger.json file; we are using the python function that uses the binding. The function is looking for the name parameter in a query string or into the body of an http request as follows.
Code:
{
"scriptFile": "__init__.py",
"disabled": false,
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req"
},
{
"type": "http",
"direction": "out",
"name": "$return"
} ]
}
Output:
The following will explain the properties of the binding configuration which we are setting in httptrigger.json file as follows.
- Type – This is defined as type of trigger.
- Direction – We have set direction as in.
- Name – Variable name which was used in http trigger.
- authLevel – It will define the authorization level.
Blob
Blob storage is the binding part of the extension bundle that we specified in the host.json file. If the bundles are not installed, we must modify this bundle to change the binding version. We can add the extension version from the bundle of extensions by adding or replacing the below code into the host.json file as follows.
Code:
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[3.3.0, 4.0.0)"
}
}
Output:
Examples of Azure Function Triggers
Below are the examples of azure function triggers as follows:
Example #1
In the below example, we are defining the http trigger.
Code:
{
"dataType": "binary",
"type": "httpTrigger",
"name": "req",
"direction": "in"
}
Output:
Example #2
In the below example, we are defining the blob trigger as follows.
Code:
{
"dataType": "binary",
"type": "blob",
"name": "req",
"direction": "in"
}
Output:
Example #3
In the below example, we are defining the queue trigger as follows.
Code:
{
"dataType": "binary",
"type": "queue",
"name": "req",
"direction": "in"
}
Output:
Conclusion
The GET request is obtained for the most recent transactions into the banking applications. The binding cannot coexist without the function triggers being defined in Azure. Azure function triggers are the parts of the function stack that tell Azure how our function will be invoked. The most common functions in azure function triggers are timer, blob, and http.
Recommended Articles
This is a guide to Azure Function Triggers. Here we discuss the introduction, types of azure functions trigger, and examples. You can also look at the following articles to learn more –