Functions and Triggers
What are Triggers and Bindings?
Azure Functions are event-driven compute services. A trigger defines how a function is invoked. When a trigger event occurs, the Azure Functions runtime executes the function.
Bindings provide a declarative way to connect to other Azure services and to configure the input and output data for a function. They simplify development by abstracting away the complexities of connecting to these services.
Key Concept: Triggers start a function, and bindings manage the data flowing into and out of it.
Trigger Types
Azure Functions supports various trigger types, each designed to respond to specific events. The most common ones include:
- Timer Trigger: Executes a function on a schedule, similar to cron jobs.
- HTTP Trigger: Invokes a function in response to an HTTP request.
- Blob Trigger: Executes a function when a new or updated blob is detected in Azure Blob Storage.
- Queue Trigger: Runs a function when a message is added to an Azure Storage Queue.
- Service Bus Trigger: Responds to messages from Azure Service Bus queues or topics.
- Event Hub Trigger: Processes events from Azure Event Hubs.
- Cosmos DB Trigger: Triggers when changes occur in an Azure Cosmos DB collection.
Common Triggers in Action
HTTP Trigger
This is one of the most frequently used triggers, allowing you to build web APIs and serverless backends. When an HTTP request (GET, POST, etc.) is made to the function's URL, the function is executed.
{
"scriptFile": "run.cs",
"entryPoint": "Run",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
Timer Trigger
Ideal for scheduled tasks, background processing, or periodic cleanups. You define a schedule using a CRON expression.
{
"scriptFile": "run.cs",
"entryPoint": "Run",
"bindings": [
{
"type": "timerTrigger",
"direction": "in",
"name": "myTimer",
"schedule": "0 */5 * * * *"
}
]
}
The schedule "0 */5 * * * *" means the function will run every 5 minutes.
Blob Trigger
Useful for processing files as they are uploaded to Azure Blob Storage. For example, resizing images, performing data validation, or archiving files.
{
"scriptFile": "run.cs",
"entryPoint": "Run",
"bindings": [
{
"name": "myBlob",
"type": "blobTrigger",
"direction": "in",
"path": "samples-workitems/{name}",
"connection": "AzureWebJobsStorage"
}
]
}
This configuration will trigger the function whenever a new blob is added to the samples-workitems container. The {name} part captures the blob's name.
Input and Output Bindings
Bindings work hand-in-hand with triggers. While a trigger starts the function, input bindings provide data from other services into your function, and output bindings send data from your function to other services.
Example: HTTP Trigger with Output Binding
This example shows an HTTP trigger that also has an output binding to send a response back to the client.
{
"scriptFile": "run.cs",
"entryPoint": "Run",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [ "get" ]
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"name": "outputBlob",
"type": "blob",
"direction": "out",
"path": "output-container/{name}.txt",
"connection": "AzureWebJobsStorage"
}
]
}
In this scenario, the function is triggered by an HTTP request. It can read data from the req input binding, process it, and then use the res output binding to send an HTTP response. Additionally, it can write data to output-container using the outputBlob binding.
| Direction | Type | Purpose |
|---|---|---|
| In | Trigger | Starts the function execution. |
| In | Binding | Provides data to the function from external services. |
| Out | Binding | Sends data from the function to external services. |
Choosing the Right Trigger
The choice of trigger depends entirely on the event you want your function to respond to. Consider the following:
- Scheduled tasks: Timer Trigger.
- Web APIs/Webhooks: HTTP Trigger.
- File processing in storage: Blob Trigger.
- Message queue processing: Queue Trigger or Service Bus Trigger.
- Real-time data streaming: Event Hub Trigger.
- Database changes: Cosmos DB Trigger.
Tip: Many functions can be triggered by multiple event types or use a combination of triggers and bindings to create complex workflows.