Azure Functions Python Queue Trigger
This document explains how to use the Azure Queue Storage output binding in an Azure Functions Python application. The Queue Storage output binding writes a message to an Azure Storage queue.
The Azure Queue Storage output binding is configured in the function.json file. For Python, you can also define output bindings using decorators in your function code.
Prerequisites
- An Azure account with an active subscription. If you don't have one, create a free account.
- A Storage account.
- A queue within your storage account.
Queue Storage Output Binding Configuration
You can define the queue binding in your function.json file. The following example shows a function that receives an HTTP request, creates a message, and sends it to a queue:
{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get",
        "post"
      ],
      "authLevel": "function"
    },
    {
      "name": "outputQueue",
      "type": "queue",
      "direction": "out",
      "queueName": "my-output-queue",
      "connection": "AzureWebJobsStorage"
    },
    {
      "name": "res",
      "type": "http",
      "direction": "out"
    }
  ]
}
Python Code with Decorator
Alternatively, for Python, you can use decorators. This approach is often preferred for its clarity and integration within the code itself.
import logging
import azure.functions as func
def main(req: func.HttpRequest, outputQueue: func.Out[str]) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    message_body = "Hello from Azure Functions!"
    try:
        req_body = req.get_json()
    except ValueError:
        pass
    else:
        message_body = req_body.get('message', "Hello from Azure Functions!")
    # Write the message to the output queue
    outputQueue.set(message_body)
    logging.info(f"Message sent to queue: {message_body}")
    return func.HttpResponse(
         "Message sent to queue successfully.",
         status_code=200
    )
In this example:
- outputQueue: func.Out[str]defines an output binding named- outputQueuethat will write strings.
- outputQueue.set(message_body)sends the message to the queue.
Configuring the Connection String
The connection property in function.json (or implied by the decorator) refers to an application setting that contains the connection string for your Azure Storage account. By default, it uses a setting named AzureWebJobsStorage. This setting is typically configured in your Function App's application settings in the Azure portal or in a local.settings.json file for local development.
local.settings.json example:
            {
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=YOUR_STORAGE_ACCOUNT_NAME;AccountKey=YOUR_STORAGE_ACCOUNT_KEY;EndpointSuffix=core.windows.net",
    "FUNCTIONS_WORKER_RUNTIME": "python"
  }
}
Binding Parameters
When using the Queue output binding, you can specify the following parameters:
- name: The name of the variable in your code function that represents the queue message.
- type: Must be set to queue.
- direction: Must be set to out.
- queueName: The name of the queue to which messages are written.
- connection: The name of the application setting that contains the Azure Storage connection string.
queueName doesn't exist, Azure Functions will create it for you.
            Error Handling
Ensure proper error handling, especially when retrieving data from the incoming request or when setting the queue message. The example above includes a basic `try-except` block for JSON parsing.
Next Steps
- Explore other Azure Functions bindings like Blob Storage and Cosmos DB.
- Learn more about monitoring Azure Functions.