You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Triggers and bindings are the mechanism that connects Azure Functions to external services. A trigger defines how a function is invoked. Bindings provide a declarative way to read from and write to external data sources without writing boilerplate connection code. Understanding triggers and bindings is essential to building effective serverless applications on Azure.
A trigger is the event that causes a function to execute. Every function must have exactly one trigger. The trigger defines:
Common triggers:
| Trigger | Event Source | Use Case |
|---|---|---|
| HTTP | HTTP request | REST APIs, webhooks |
| Timer | CRON schedule | Scheduled jobs, cleanup tasks |
| Queue Storage | Azure Queue message | Asynchronous task processing |
| Service Bus | Service Bus message/topic | Enterprise messaging |
| Blob Storage | Blob created/updated | File processing pipelines |
| Cosmos DB | Document change feed | Change data capture |
| Event Grid | Event Grid event | Event-driven architectures |
| Event Hub | Event Hub stream | IoT and telemetry ingestion |
| Kafka | Kafka topic message | Stream processing |
Bindings are a declarative way to connect to data without managing connections, clients, or SDKs manually. They come in two types:
Bindings are optional — a function can have zero or many input and output bindings.
Without bindings, reading from Cosmos DB requires:
// Without bindings — manual SDK usage
const { CosmosClient } = require('@azure/cosmos');
const client = new CosmosClient(process.env.COSMOS_CONNECTION);
const database = client.database('mydb');
const container = database.container('items');
const { resource } = await container.item(id, partitionKey).read();
With an input binding:
// With bindings — the document is passed directly to your function
app.http('GetItem', {
methods: ['GET'],
authLevel: 'function',
extraInputs: [cosmosInput],
handler: async (request, context) => {
const document = context.extraInputs.get(cosmosInput);
return { jsonBody: document };
}
});
The binding handles connection management, authentication, and error handling for you.
The most common trigger. Functions become REST API endpoints:
const { app } = require('@azure/functions');
app.http('GetUser', {
methods: ['GET'],
authLevel: 'function',
route: 'users/{id}',
handler: async (request, context) => {
const userId = request.params.id;
// Fetch user and return
return { jsonBody: { id: userId, name: 'Alice' } };
}
});
Key options:
route — custom URL path with parameters (/api/users/{id})authLevel — anonymous (no key), function (function key), admin (master key)methods — restrict to specific HTTP methodsSubscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.