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.


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


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


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


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

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.

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.

Next: Output Bindings