Azure Functions Queue Trigger Bindings

Introduction

The Azure Queue Storage output binding for Azure Functions enables you to add messages to an Azure Queue Storage queue. This binding can be used in both input and output modes. When used as an output binding, your function is triggered by a message in a queue and can then add messages to another queue. This is a powerful pattern for implementing asynchronous processing, work queues, and communication between different parts of your application.

This document focuses on the output binding scenario for queue triggers, allowing your function to write messages to a queue.

Supported Languages

The queue trigger binding is supported in the following languages:

Output Binding Configuration

To configure the queue output binding, you define it in your function's function.json file (for JavaScript/TypeScript/Python) or using attributes in your code (for C#/Java).

function.json Example (JavaScript/TypeScript)

{
  "bindings": [
    {
      "name": "myQueueItem",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "myqueue-items",
      "connection": "AzureWebJobsStorage"
    },
    {
      "name": "outputQueueItem",
      "type": "queue",
      "direction": "out",
      "queueName": "processed-messages",
      "connection": "AzureWebJobsStorage"
    }
  ],
  "scriptFile": "index.js"
}

function.json Example (Python)

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "msg",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "myqueue-items",
      "connection": "AzureWebJobsStorage"
    },
    {
      "name": "outputQueue",
      "type": "queue",
      "direction": "out",
      "queueName": "processed-messages",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

C# Attribute Example

using Microsoft.Azure.WebJobs;

namespace MyNamespace
{
    public static class QueueTriggerAndOutput
    {
        [FunctionName("QueueTriggerAndOutput")]
        public static void Run(
            [QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem,
            [Queue("processed-messages", Connection = "AzureWebJobsStorage")] out string outputQueueItem)
        {
            // Process myQueueItem and set outputQueueItem
            outputQueueItem = $"Processed: {myQueueItem}";
        }
    }
}

Java Annotation Example

import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;

public class QueueTriggerAndOutput {

    @FunctionName("QueueTriggerAndOutput")
    public void run(
            @QueueTrigger(name = "myQueueItem", queueName = "myqueue-items", connection = "AzureWebJobsStorage") String myQueueItem,
            @Queue(name = "outputQueue", queueName = "processed-messages", connection = "AzureWebJobsStorage") OutputBinding<String> outputQueue,
            final ExecutionContext context) {

        context.getLogger().info("Java Queue trigger function processed: " + myQueueItem);
        outputQueue.setValue("Processed via Java: " + myQueueItem);
    }
}

Binding Properties

Property Description Required Default
name The name of the variable in your function code that represents the queue message. Yes N/A
type Must be queue for an output binding. Yes N/A
direction Must be out for an output binding. Yes N/A
queueName The name of the queue to write messages to. Yes N/A
connection The name of an app setting that contains the Azure Storage connection string. Yes N/A

Usage in Code

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

JavaScript Example

module.exports = async function (context, myQueueItem) {
    context.log('JavaScript queue trigger function processed work item', myQueueItem);

    const processedMessage = `Processed: ${myQueueItem}`;
    context.bindings.outputQueueItem = processedMessage; // Assign to the output binding name

    context.log('Sent message to output queue:', processedMessage);
};

Python Example

import logging

import azure.functions as func


def main(msg: func.QueueMessage, outputQueue: func.Out[str]) -> None:
    logging.info('Python queue trigger function processed a queue item: %s',
                 msg.get_body().decode('utf-8'))

    message_body = msg.get_body().decode('utf-8')
    processed_message = f"Processed: {message_body}"
    outputQueue.set(processed_message) # Set the value for the output binding
    logging.info('Sent message to output queue: %s', processed_message)
Tip: For output bindings, the value you assign to the binding variable (e.g., context.bindings.outputQueueItem or outputQueue.set()) becomes the content of the queue message.

Important Considerations

Note: When using the queueTrigger as an input binding, it receives the message content. When used as an output binding, your function writes a message to a queue. This document primarily addresses the output binding functionality.

Related Topics