Azure Logo Azure Functions

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

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:

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:

Tip: If the queue specified by 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