Azure Functions

Documentation

Queue Output Bindings

Learn how to use queue output bindings to send messages to Azure Storage Queues from your Azure Functions.

Overview

Output bindings allow your Azure Function to interact with other Azure services and external systems without requiring explicit SDK code in your function. Queue output bindings are used to send messages to an Azure Storage Queue.

When you define a queue output binding, you can simply return a value from your function, or assign a value to the output binding parameter, and Azure Functions runtime will automatically handle sending that message to the configured queue.

Configuration

Queue output bindings are configured in the function.json file for your function.

function.json Example

function.json

{
  "bindings": [
    {
      "name": "msg",
      "type": "queue",
      "direction": "out",
      "queueName": "myoutputqueue",
      "connection": "AzureWebJobsStorage"
    }
  ]
}
            

In this example:

Using the Binding in Code

C# Example

C#

using Microsoft.Azure.Functions.Worker;

public static class QueueOutputFunction
{
    [Function("QueueOutputFunction")]
    [QueueOutput("myoutputqueue")]
    public static string Run(
        [QueueTrigger("myinputqueue")] string myQueueItem,
        FunctionContext context)
    {
        var logger = context.GetLogger();
        logger.LogInformation($"C# Queue trigger function processed: {myQueueItem}");

        string responseMessage = $"Message '{myQueueItem}' processed and sent to output queue.";
        return responseMessage; // This string will be sent to the 'myoutputqueue'
    }
}
            

JavaScript (Node.js) Example

JavaScript (Node.js)

const { app } = require('@azure/functions');

app.storageQueue('queueOutputFunction', {
    queueName: 'myinputqueue',
    connection: 'AzureWebJobsStorage',
    handler: async (queueItem, context) => {
        context.log('JavaScript queue trigger function processed work item', queueItem);

        // Example of sending a message to an output queue
        // The output binding 'msg' must be defined in function.json
        context.bindings.msg = `Processed: ${queueItem}`;
        context.log('Message sent to output queue.');
    }
});
            

In the JavaScript example, the output binding is referred to via context.bindings.msg, where msg matches the "name" property in the function.json.

Python Example

Python

import logging
import azure.functions as func

def main(myQueueItem: str, msg: func.Out[str]) -> None:
    logging.info('Python queue trigger function processed a work item: %s', myQueueItem)
    msg.set(f"Processed: {myQueueItem}")
            

In the Python example, the output binding is represented by the msg: func.Out[str] parameter, where msg matches the "name" property in the function.json.

Key Concepts

Note: The connection setting in function.json points to an application setting that holds your Azure Storage connection string. Ensure this setting is correctly configured in your Azure Function App's configuration.
Warning: Never hardcode connection strings directly in your function code or function.json. Always use application settings or environment variables.

Further Reading