Azure Functions

Azure Functions Storage Queue Output Bindings

Output bindings for Azure Storage Queues allow your Azure Functions to write messages to a queue. This is a common pattern for asynchronous processing, where one function's output becomes the input for another.

How it works

When you configure an output binding for a storage queue in your function, you specify the queue name and the connection string. Your function code can then simply return a value, or assign a value to the output binding parameter, and Azure Functions will automatically serialize and send that value as a new message to the specified queue.

Configuration

Output bindings are typically configured in the function.json file.

Example function.json


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

In this example:

Programming Model

The way you interact with the output binding depends on your function's programming language.

Python Example

In Python, the output binding is represented by a parameter marked with the out direction in function.json. You can assign a value to this parameter, and it will be sent to the queue.


import logging
import azure.functions as func

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

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        message_content = f"Hello, {name}! This is a message sent to the output queue."
        msg.set(message_content)
        return func.HttpResponse(
             f"Message sent to output queue: '{message_content}'",
             status_code=200
        )
    else:
        return func.HttpResponse(
             "Please pass a name on the query string or in the request body",
             status_code=400
        )
            

C# Example

In C#, you typically use an out parameter of type IAsyncCollector<string> or similar.


using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

public static class QueueOutputExample
{
    [FunctionName("QueueOutputExample")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        [Queue("myoutputqueue", Connection = "AzureWebJobsStorage")] IAsyncCollector<string> msg,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        string name = req.Query["name"];
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
        name = name ?? data?.name;

        string responseMessage = string.IsNullOrEmpty(name)
            ? "Please pass a name on the query string or in the request body"
            : $"Hello, {name}! This is a message sent to the output queue.";

        if (!string.IsNullOrEmpty(name))
        {
            await msg.AddAsync($"Message for {name} sent from C# function.");
        }

        return new OkObjectResult(responseMessage);
    }
}
            

Binding Parameters Explained

Parameter Description Required
type Must be queue. Yes
direction Must be out. Yes
name The name of the variable in your function code that represents the output binding. Yes
queueName The name of the Azure Storage Queue. Yes
connection The name of an app setting containing the Azure Storage connection string. Defaults to AzureWebJobsStorage. No (defaults to AzureWebJobsStorage)
Note: When sending messages, ensure the data you are sending can be serialized into a string. Azure Functions handles basic types and JSON serialization automatically for many languages.
Key Benefit: Output bindings simplify the process of sending data to queues, abstracting away the complexities of the Azure Storage SDK and allowing you to focus on your business logic.

Common Scenarios