Azure Functions Bindings

Queue Bindings in Azure Functions

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.

How Queue Bindings Work

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).

Input Bindings (Triggering Functions)

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.

Automatic Execution

Your function code doesn't need to poll the queue. Azure Functions runtime handles the polling and triggers your function on new messages.

Message Handling

The message content is deserialized and provided directly to your function, simplifying processing.

Scalability

Azure Functions can scale automatically based on the number of messages in the queue.

Output Bindings (Sending Messages)

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.

Decoupling

Send messages to a queue to decouple components and allow asynchronous communication.

Simple Integration

Return a value from your function or use a parameter marked as an output binding to send a message.

Configuration

Queue bindings are typically configured in the function.json file for your Azure Function. The following examples illustrate common configurations.

Example: Input Binding (Queue Trigger)

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'))

Example: Output Binding (Queue Output)

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')}")

Key Concepts

Best Practices