Azure Functions Reference
This document provides a comprehensive reference for Azure Functions, covering triggers, bindings, configuration, runtime details, and best practices.
Triggers
Triggers define how a Function is invoked. They are the entry points for your function's execution. Azure Functions supports a wide variety of triggers:
- HTTP Trigger: Invoked by an HTTP request.
- Timer Trigger: Runs on a schedule defined by a CRON expression.
- Blob Trigger: Executes when a blob is created or updated in Azure Blob Storage.
- Queue Trigger: Fires when a new message is available in an Azure Storage Queue.
- Service Bus Trigger: Reacts to messages arriving in an Azure Service Bus Queue or Topic.
- Event Hubs Trigger: Processes events from Azure Event Hubs.
- Cosmos DB Trigger: Responds to changes in a Cosmos DB collection.
- Event Grid Trigger: Subscribes to events from Azure Event Grid.
HTTP Trigger Example
{
"scriptFile": "run.csx",
"entryPoint": "Run",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
Bindings
Bindings allow you to declaratively connect to other Azure services and external data sources. They reduce the amount of boilerplate code you need to write.
Input and Output Bindings
Bindings can be configured as either input or output bindings. Input bindings provide data to your function, while output bindings send data from your function to another service.
| Binding Type | Description | Direction |
|---|---|---|
| Azure Blob Storage | Read/write blobs to Azure Blob Storage. | Input/Output |
| Azure Table Storage | Read/write entities to Azure Table Storage. | Input/Output |
| Azure Cosmos DB | Read documents from or write documents to Azure Cosmos DB. | Input/Output |
| Service Bus Queue/Topic | Send messages to or receive messages from Service Bus. | Input/Output |
| Twilio | Send SMS messages via Twilio. | Output |
Cosmos DB Output Binding Example
{
"scriptFile": "run.js",
"entryPoint": "run",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req"
},
{
"type": "cosmosDB",
"direction": "out",
"name": "outputDocument",
"databaseName": "ToDoList",
"collectionName": "Items",
"connectionStringSetting": "CosmosDBConnection"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
Configuration
Azure Functions can be configured using various methods:
function.json: Defines triggers and bindings for a specific function.host.json: Configures global settings for the Functions host, such as logging levels and extensions.- Application Settings: Environment variables for storing connection strings, API keys, and other configuration values.
function.json or host.json.
Runtime
Azure Functions are supported across multiple runtime stacks, allowing you to use your preferred programming language:
- .NET
- Node.js
- Python
- Java
- PowerShell
- Custom Handlers (for other languages)
Runtime Versions
You can specify the runtime version in your host.json file. For example:
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[3.*, 4.0.0)"
}
}
Best Practices
To build robust and efficient Azure Functions, consider the following best practices:
- Keep Functions Small and Focused: Each function should perform a single task.
- Use Bindings Effectively: Leverage bindings to simplify integrations with other services.
- Manage Dependencies: Use package managers (npm, pip, NuGet) to manage your function's dependencies.
- Handle Errors Gracefully: Implement proper error handling and logging.
- Optimize for Cold Starts: For language runtimes sensitive to cold starts (like Python and Node.js), consider strategies like keeping functions warm or using premium plans.
- Secure Your Functions: Use appropriate authentication and authorization mechanisms (e.g., function keys, OAuth).