Azure Functions: Triggers and Bindings
Welcome to the Azure Functions tutorial on triggers and bindings. This guide will walk you through the core concepts that make Azure Functions so powerful and flexible.
What are Triggers?
A trigger is what causes a function to execute. Each Azure Function has a single trigger. Triggers define how a function is invoked and provide input data to the function.
Common triggers include:
- HTTP Trigger: Executes when an HTTP request is received.
- Timer Trigger: Executes on a schedule defined by a cron expression.
- Blob Trigger: Executes when a blob is added or updated in Azure Blob Storage.
- Queue Trigger: Executes when a message is added to an Azure Storage Queue.
- Event Grid Trigger: Executes in response to events published by Azure Event Grid.
What are Bindings?
Bindings allow your function to connect to other Azure services and SaaS offerings without requiring you to write complex integration code. They represent an abstraction layer that makes it easy to declaratively connect to various data sources and services.
Bindings come in two main forms:
- Input Bindings: Provide data to your function from a service.
- Output Bindings: Send data from your function to a service.
Key Benefits
- Declarative: Define connections in your function.json file, not in code.
- Reusable: A single binding can be used by multiple functions.
- Simplified Code: Focus on your business logic, not service integration.
Example: HTTP Trigger with an Output Binding
Let's consider a simple example where an HTTP-triggered function writes a message to an Azure Storage Queue.
Function.json (Configuration)
{
"scriptFile": "run.py",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"type": "queue",
"direction": "out",
"name": "outputQueueItem",
"queueName": "my-output-queue",
"connection": "AzureWebJobsStorage"
}
]
}
Python Code (Example Logic)
import logging
import azure.functions as func
def main(req: func.HttpRequest, outputQueueItem: func.Out[str]) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
message = f"Hello, {name}! This message is being sent to the queue."
outputQueueItem.set(message)
return func.HttpResponse(
"Please pass a name on the query string or in the request body. Message sent to queue.",
status_code=200
)
else:
return func.HttpResponse(
"Please pass a name on the query string or in the request body.",
status_code=400
)
Common Service Integrations
Azure Blob Storage
Interact with files and data in Blob Storage.
- Triggers: Blob Trigger
- Bindings: Blob Input/Output
Azure Cosmos DB
Read from and write to NoSQL documents.
- Bindings: Cosmos DB Input/Output
Azure Service Bus
Work with queues and topics for reliable messaging.
- Triggers: Service Bus Trigger
- Bindings: Service Bus Input/Output
Azure Event Hubs
Ingest high-throughput telemetry data.
- Triggers: Event Hubs Trigger
- Bindings: Event Hubs Input/Output
Azure Table Storage
Store and query large amounts of structured, non-relational data.
- Bindings: Table Input/Output
SendGrid
Send emails programmatically.
- Bindings: SendGrid Output
Next Steps
Explore the HTTP Trigger documentation and the full list of bindings to see how you can integrate with your favorite services.