Azure Functions Queue Output Bindings

This document describes how to configure and use Azure Queue Storage output bindings in Azure Functions.

Overview

Output bindings for Azure Queue Storage allow your Azure Functions to write messages to a queue without explicitly writing SDK code. This simplifies your function logic and promotes a declarative approach to integrating with Azure Storage.

Configuration

To use a queue output binding, you define it in your function's function.json file or using attributes in your code.

function.json Example

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "authLevel": "function",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "name": "outputQueueItem",
      "type": "queue",
      "direction": "out",
      "queueName": "my-output-queue",
      "connection": "AzureWebJobsStorage"
    },
    {
      "name": "res",
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}

Attribute Example (C#)

using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using System.Net;

namespace FunctionApp1
{
    public static class HttpTriggerQueueOutput
    {
        [Function("HttpTriggerQueueOutput")]
        public static async Task Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
            [Queue("my-output-queue", Connection = "AzureWebJobsStorage")] out string outputQueueItem,
            FunctionContext context)
        {
            var logger = context.GetLogger("HttpTriggerQueueOutput");
            logger.LogInformation("HTTP trigger function processed a request.");

            string message = "Hello from Azure Functions Queue Output!";
            outputQueueItem = message; // Assign message to the output binding

            var response = req.CreateResponse(HttpStatusCode.OK);
            response.WriteString("Message sent to queue.");

            return response;
        }
    }
}

Binding Properties

The following properties are available for queue output bindings:

Property Description Required
name The name of the variable that represents the message in your function code. Yes
type Must be set to queue. Yes
direction Must be set to out. Yes
queueName The name of the Azure Queue Storage queue to write to. If the queue doesn't exist, it will be created. Yes
connection The name of an app setting that contains your Azure Storage connection string. Defaults to AzureWebJobsStorage. No

Using the Output Binding

Once configured, you can assign a string value to the output binding variable in your function. This value will be automatically sent as a message to the specified queue.

Python Example

In Python, the output binding is typically defined in function.json. You can then access it in your code using the set method of the binding object.

import logging
import azure.functions as func

def main(req: func.HttpRequest, outputQueueItem: func.Out[str]) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    message_to_send = "Hello from Python Azure Functions!"
    outputQueueItem.set(message_to_send)

    return func.HttpResponse(
         "Message sent to queue.",
         status_code=200
    )

Message Content

The output binding expects a string or a JSON serializable object. For complex data, ensure it's properly serialized before assigning it to the output binding.

Important: Azure Queue Storage messages have a size limit of 64 KB. Ensure your messages do not exceed this limit.

Related Topics