Azure Functions Queue Bindings
Queue bindings enable your Azure Functions to interact with Azure Queue Storage. They provide triggers for when a new message is available in a queue and input/output bindings for reading from and writing to queues.
Trigger: Queue Trigger
A queue trigger starts your function when a new message arrives in an Azure Queue Storage queue. The message content is passed as input to your function.
Example (JavaScript):
module.exports = async function (context, myQueueItem) {
context.log('JavaScript queue trigger function processed work item', myQueueItem);
// Process the message content in myQueueItem
};
Example (C#):
// Assume Startup.cs configures the queue connection
// public static class QueueTriggerFunction
// {
// [Function("QueueTriggerFunction")]
// public static void Run(
// [QueueTrigger("myqueue-items")] string myQueueItem,
// FunctionContext context)
// {
// context.Logger.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
// }
// }
Input Binding: Queue Input
A queue input binding allows you to read a single message from a queue as input to your function. Unlike the trigger, this is typically used for polling a queue within an existing function execution.
Example (JavaScript):
module.exports = async function (context, req, myQueueItem) {
context.log('Received queue message:', myQueueItem);
// Use myQueueItem
context.res = {
body: `Processed message: ${myQueueItem}`
};
};
Output Binding: Queue Output
A queue output binding allows your function to send a message to an Azure Queue Storage queue. You return the message content, or pass it to a context object.
Example (JavaScript):
module.exports = async function (context, req) {
const messageToSend = req.body && req.body.message;
if (messageToSend) {
context.bindings.outputQueueItem = messageToSend;
context.res = {
status: 200,
body: "Message sent to queue."
};
} else {
context.res = {
status: 400,
body: "Please provide a message to send."
};
}
};
Example (C#):
// public static class QueueOutputFunction
// {
// [Function("QueueOutputFunction")]
// [QueueOutput("output-queue")]
// public static string Run(
// [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req,
// FunctionContext context)
// {
// string message = req.ReadAsStringAsync().Result;
// context.Logger.LogInformation($"Sending message to output queue: {message}");
// return message;
// }
// }
Configuration
Queue bindings require configuration to connect to your Azure Storage account. This is typically done in the function.json file (for non-language specific configuration) or through attributes in your code (e.g., C#).
function.json Example (Trigger):
{
"scriptFile": "index.js",
"bindings": [
{
"name": "myQueueItem",
"type": "queueTrigger",
"direction": "in",
"queueName": "myqueue-items",
"connection": "AzureWebJobsStorage"
}
]
}
function.json Example (Output):
{
"scriptFile": "index.js",
"bindings": [
{
"name": "outputQueueItem",
"type": "queue",
"direction": "out",
"queueName": "output-queue",
"connection": "AzureWebJobsStorage"
}
]
}
The connection property refers to a setting in your Azure Functions application settings, typically named AzureWebJobsStorage, which holds your Azure Storage connection string.
Key Properties
| Property | Description | Required |
|---|---|---|
name |
The name of the variable that represents the message in your code. | Yes |
type |
queueTrigger, queue. |
Yes |
direction |
in for triggers and input bindings, out for output bindings. |
Yes |
queueName |
The name of the queue to interact with. | Yes |
connection |
The name of the application setting that contains the Azure Storage connection string. Defaults to AzureWebJobsStorage. |
No (defaults to AzureWebJobsStorage) |
Best Practices
- Use clear and descriptive
queueNamevalues. - Ensure your storage connection string is securely managed in application settings.
- Handle potential errors and poison messages appropriately.
- Consider message idempotency for queue triggers to prevent duplicate processing.