Azure Functions Triggers
Triggers are how Azure Functions are invoked. A trigger defines how a function is activated. Each function must have exactly one trigger.
Understanding Triggers
Triggers come in various types, allowing your functions to respond to different events:
- Event-driven triggers: Respond to events from other Azure services or external sources.
- Scheduled triggers: Execute on a defined schedule.
- Manual triggers: Executed on demand.
Common Trigger Types
Here are some of the most frequently used trigger types:
HTTP Trigger
The HTTP trigger allows your function to be invoked via an HTTP request. This is ideal for building web APIs, webhooks, and microservices.
When an HTTP trigger is configured, your function can receive request data and respond with HTTP status codes and responses.
// Example: C# HTTP Trigger function
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
public static class HttpTriggerFunction
{
[FunctionName("HttpTriggerFunction")]
public static async Task Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}! This HTTP triggered function executed successfully.";
return new OkObjectResult(responseMessage);
}
}
Timer Trigger
The Timer trigger allows you to run functions on a schedule, similar to cron jobs. You can define the schedule using a CRON expression.
// Example: Timer trigger configuration (function.json)
{
"scriptFile": "../run.py",
"bindings": [
{
"name": "myTimer",
"type": "timerTrigger",
"direction": "in",
"schedule": "0 */5 * * * *" // Runs every 5 minutes
}
]
}
Blob Trigger
The Blob trigger monitors a blob container. When a new or updated blob is detected, the function is executed. This is useful for processing file uploads or data changes.
Queue Trigger
The Queue trigger listens to an Azure Storage Queue. When a new message is added to the queue, the function is invoked.
Event Grid Trigger
The Event Grid trigger allows your function to respond to events published by Azure Event Grid, enabling event-driven architectures across Azure services.
Choosing the Right Trigger
The selection of a trigger depends entirely on the event you want to respond to:
- For webhooks or REST APIs: HTTP Trigger
- For scheduled tasks: Timer Trigger
- For processing file uploads: Blob Trigger
- For asynchronous message processing: Queue Trigger
- For responding to a wide range of Azure service events: Event Grid Trigger
You can also combine triggers with input and output bindings to create powerful serverless workflows.
Explore HTTP Triggers Learn About Timer Triggers