Azure Functions Triggers
This article explains the different types of triggers you can use with Azure Functions to invoke your function code. Triggers are the primary way to cause a function to execute. Each trigger defines how a function is invoked and the input parameters it receives.
Overview of Triggers
A trigger is a specific type of Azure Functions binding that executes your function code in response to an event. Triggers define the event that starts your function. For example, a timer trigger runs your function on a schedule, while an HTTP trigger runs your function when an HTTP request is received.
When a trigger fires, it passes data to your function as input. This input data is represented by the function's parameters. The type of data depends on the trigger type.
Common Trigger Types
HTTP Trigger
The HTTP trigger allows your function to be invoked via an HTTP request. This is useful for building web APIs, webhooks, or any scenario where you need to respond to an incoming web request.
- Event: An incoming HTTP request.
- Input: An object representing the HTTP request (e.g.,
HttpRequest). - Use cases: REST APIs, webhook receivers, command-and-control endpoints.
// Example C# signature
public static void Run(HttpRequest req, ILogger log)
{
log.LogInformation("HTTP trigger function processed a request.");
// ... function logic
}
Timer Trigger
The Timer trigger runs your function on a defined schedule, typically using a CRON expression. This is ideal for periodic tasks or scheduled operations.
- Event: Time-based schedule.
- Input: A timer object (e.g.,
TimerInfo). - Use cases: Scheduled cleanup jobs, data aggregation, periodic reporting.
// Example C# signature
[FunctionName("TimerTriggerCSharp")]
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
// ... function logic
}
Blob Trigger
The Blob trigger executes your function when a new or updated blob is detected in an Azure Storage blob container. This is useful for processing files as they are uploaded.
- Event: Creation or update of a blob in Azure Blob Storage.
- Input: The content of the blob (e.g.,
Streamorbyte[]). - Use cases: Image resizing, data validation, file transformation.
// Example C# signature
public static void Run([BlobTrigger("samples-workitems/{name}", Connection = "AzureWebJobsStorage")] Stream myBlob, string name, ILogger log)
{
log.LogInformation($"C# Blob trigger function processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
// ... function logic
}
Queue Trigger
The Queue trigger executes your function when a new message arrives in an Azure Storage queue. This is a common pattern for asynchronous processing and decoupling services.
- Event: A new message in an Azure Storage queue.
- Input: The message content (e.g.,
string). - Use cases: Background processing, task queuing, inter-service communication.
// Example C# signature
public static void Run([QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem, ILogger log)
{
log.LogInformation($"Dequeued: {myQueueItem}");
// ... function logic
}
Service Bus Trigger
The Service Bus trigger executes your function in response to messages arriving on an Azure Service Bus queue or topic. This offers more advanced messaging capabilities than Storage queues.
- Event: A new message in an Azure Service Bus queue or topic.
- Input: The message content (e.g.,
string,byte[], or specific Service Bus types). - Use cases: Reliable messaging, order processing, event-driven architectures.
Event Hubs Trigger
The Event Hubs trigger processes events from Azure Event Hubs, a highly scalable data streaming platform. This is suitable for ingesting and processing large volumes of real-time data.
- Event: Events arriving in an Azure Event Hubs partition.
- Input: An array of event data objects.
- Use cases: IoT data ingestion, real-time analytics, log collection.
Cosmos DB Trigger
The Cosmos DB trigger executes your function when documents are created or updated in a Cosmos DB collection. It uses the Change Feed to detect changes.
- Event: Document changes in a Cosmos DB collection.
- Input: A collection of changed documents.
- Use cases: Real-time data processing, notifications, data synchronization.
Configuring Triggers
Triggers are configured in the function.json file for your function (or via attributes in code for compiled languages like C#). The configuration specifies the trigger type, the event source (e.g., queue name, blob path), and any necessary connection strings.
Important Considerations:
When designing your functions, consider the trigger that best suits your event source. The choice of trigger can significantly impact how your function is invoked, its scalability, and the data it receives.