Azure Functions Queue Input Bindings
Queue input bindings enable your Azure Functions to read messages from Azure Storage Queues. When a function is triggered by a queue message, the binding automatically retrieves the message and makes its content available to your function code.
How it Works
When you define a queue input binding, you specify the name of the queue to read from. Azure Functions runtime monitors the specified queue. When a new message arrives, the runtime retrieves it. For input bindings, the function code receives the message content. For trigger bindings, the message content is the primary event data that starts function execution. If your function is triggered by a queue message, you can also use an input binding to retrieve additional messages or even a specific message from the same queue or a different queue.
Supported Queue Types
Azure Functions supports input bindings for:
- Azure Storage Queues: The standard queue service provided by Azure Storage.
Configuration
Queue input bindings are configured in the function.json file (for JavaScript, Python, and other non-.NET languages) or through attributes in your code (for C# and other .NET languages).
function.json Example (JavaScript/TypeScript)
{
"scriptFile": "index.js",
"bindings": [
{
"name": "myQueueItem",
"type": "queueTrigger",
"direction": "in",
"queueName": "myqueue-items",
"connection": "AzureWebJobsStorage"
},
{
"name": "queueMessage",
"type": "queue",
"direction": "in",
"queueName": "another-input-queue",
"connection": "AzureWebJobsStorage",
"accessRights": "peek"
}
]
}
.NET Attribute Example (C#)
using Microsoft.Azure.Functions.Worker;
using Azure.Storage.Queues.Models;
public static class QueueInputFunction
{
[Function("QueueInputFunction")]
public static void Run(
[QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem,
[Queue("another-input-queue", Connection = "AzureWebJobsStorage", AccessRights = QueueAccessRights.Peek)] QueueMessage queueMessage)
{
Console.WriteLine($"Trigger function processed: {myQueueItem}");
Console.WriteLine($"Input binding message: {queueMessage.MessageText}");
}
}
Binding Parameters
Here are the key properties for configuring a queue input binding:
| Property | Description | Required |
|---|---|---|
type |
Must be set to queue for an input binding. |
Yes |
direction |
Must be set to in for an input binding. |
Yes |
name |
The name of the variable in your function code that will hold the queue message content. | Yes |
queueName |
The name of the storage queue to read from. | Yes |
connection |
The name of an app setting that contains the storage account connection string. Defaults to AzureWebJobsStorage. |
No |
accessRights |
Specifies the access rights for the queue. For input bindings, typically peek is sufficient if you only need to read the message without dequeuing it immediately. Other options include none. read is also implicitly supported for input bindings. Defaults to none (meaning the message is not deleted after processing by this binding). |
No |
Data Types
The content of the queue message can be bound to various data types in your function code:
- String: The raw message content as a string.
- JSON Object: If the message content is valid JSON, it can be automatically deserialized into an object (e.g.,
JObjectin C#, or a JavaScript object). - Byte Array: For binary messages.
- POCOs (Plain Old CLR Objects): In .NET, you can bind directly to strongly-typed objects if the message content is JSON that matches the object's structure.
peek access right ensures the message is only read, not dequeued.
Advanced Scenarios
- Multiple Input Bindings: You can configure multiple queue input bindings in a single function to read from different queues.
- Batch Processing: For higher throughput, consider using queue trigger bindings that support batching to process multiple messages at once. Input bindings typically process one message at a time.
By leveraging queue input bindings, you can efficiently integrate your Azure Functions with Azure Storage Queues, building robust and scalable messaging-driven applications.