Azure Functions provide powerful input and output bindings that allow you to easily connect your functions to other Azure services and external data sources without writing extensive integration code. Queue bindings are particularly useful for building event-driven, decoupled applications.
Queue bindings enable your Azure Function to trigger when a new message arrives in an Azure Queue Storage queue (input binding) or to write a message to a queue (output binding).
An Azure Function can be configured to automatically run when a new message is added to a specific Azure Queue Storage queue. The message content is then passed as an input parameter to your function.
Your function code doesn't need to poll the queue. Azure Functions runtime handles the polling and triggers your function on new messages.
The message content is deserialized and provided directly to your function, simplifying processing.
Azure Functions can scale automatically based on the number of messages in the queue.
You can configure your function to send a message to an Azure Queue Storage queue as an output. This is useful for chaining functions or signaling downstream processes.
Send messages to a queue to decouple components and allow asynchronous communication.
Return a value from your function or use a parameter marked as an output binding to send a message.
Queue bindings are typically configured in the function.json file for your Azure Function. The following examples illustrate common configurations.
This configuration sets up a function to trigger when a message arrives in a queue named myqueue-items.
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "msg",
"type": "queueTrigger",
"direction": "in",
"queueName": "myqueue-items",
"connection": "AzureWebJobsStorage"
}
]
}
In your function code (e.g., Python), the msg variable will contain the queue message:
import logging
import azure.functions as func
def main(msg: func.QueueMessage) -> None:
logging.info('Python queue trigger function processed a body: %s',
msg.get_body().decode('utf-8'))
This configuration defines an output binding that sends data to a queue named outputqueue.
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "msg",
"type": "queueTrigger",
"direction": "in",
"queueName": "inputqueue",
"connection": "AzureWebJobsStorage"
},
{
"name": "outputQueueMessage",
"type": "queue",
"direction": "out",
"queueName": "outputqueue",
"connection": "AzureWebJobsStorage"
}
]
}
In your function code (e.g., Python), you can set the output binding:
import logging
import azure.functions as func
def main(msg: func.QueueMessage, outputQueueMessage: func.Out[str]) -> None:
logging.info('Python queue trigger function processed a body: %s',
msg.get_body().decode('utf-8'))
outputQueueMessage.set(f"Processed message: {msg.get_body().decode('utf-8')}")
queueTrigger: Specifies the binding that triggers a function when a message arrives in an Azure Queue Storage queue.queue: Specifies the binding for sending messages to an Azure Queue Storage queue.name: The name of the parameter in your function code that represents the binding.queueName: The name of the Azure Queue Storage queue to connect to.connection: The name of an app setting that contains the connection string for your Azure Storage account. The default is AzureWebJobsStorage.