Introduction

Azure Functions provides input and output bindings for Azure Queue Storage, allowing you to easily connect your functions to queues. This enables scenarios such as processing messages from a queue, adding messages to a queue, and implementing reliable asynchronous workflows.

Queue storage is a service that stores large numbers of small messages. Using queues, you can easily build and scale applications. Messages are added to a queue and then processed by one or more clients.

Queue Trigger

The Queue trigger allows your function to be executed in response to a new message arriving in an Azure Queue Storage queue. When a message is added to the configured queue, the function is invoked with the message content as input.

Configuration

The Queue trigger is configured in the function.json file:


{
  "bindings": [
    {
      "name": "myQueueItem",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "my-input-queue",
      "connection": "AzureWebJobsStorage"
    }
  ]
}
                    
  • name: The name of the variable that will hold the queue message content in your function code.
  • type: Must be queueTrigger.
  • direction: Must be in for a trigger.
  • queueName: The name of the queue to monitor.
  • connection: The name of an app setting that contains the Azure Queue Storage connection string. Defaults to AzureWebJobsStorage.

Supported Languages

The Queue trigger is supported in the following languages:

  • C#
  • JavaScript
  • TypeScript
  • Python
  • PowerShell
  • Java

Example (JavaScript)

index.js


module.exports = async function (context, myQueueItem) {
    context.log('JavaScript queue trigger function processed work item', myQueueItem);
    // Process the message content here
};
                        

Queue Output Binding

The Queue output binding allows your function to add a message to an Azure Queue Storage queue. This is useful for scenarios where your function produces data that needs to be processed asynchronously by another service or function.

Configuration

The Queue output binding is configured in the function.json file:


{
  "bindings": [
    {
      "name": "message",
      "type": "queue",
      "direction": "out",
      "queueName": "my-output-queue",
      "connection": "AzureWebJobsStorage"
    }
  ]
}
                    
  • name: The name of the variable that will be used to send data to the queue in your function code.
  • type: Must be queue.
  • direction: Must be out for an output binding.
  • queueName: The name of the queue to add messages to.
  • connection: The name of an app setting that contains the Azure Queue Storage connection string. Defaults to AzureWebJobsStorage.

Example (C#)

MyQueueFunction.cs


using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

public static class MyQueueFunction
{
    [FunctionName("QueueOutputExample")]
    public static void Run(
        [QueueTrigger("my-input-queue")] string myQueueItem,
        [Queue("my-output-queue")] out string message,
        ILogger log)
    {
        log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");

        // Create a new message to send to the output queue
        message = $"Processed item: {myQueueItem}";

        log.LogInformation($"Message added to output queue: {message}");
    }
}
                    

Best Practices

  • Idempotency: Design your functions to be idempotent, meaning that processing the same message multiple times has the same effect as processing it once. Queue triggers automatically handle retries.
  • Connection Strings: Store your connection strings securely in application settings, not directly in your code.
  • Queue Naming: Use descriptive names for your queues (e.g., order-processing-queue, notification-queue).
  • Error Handling: Implement robust error handling within your functions to gracefully manage messages that cannot be processed. Use dead-letter queues for failed messages.

API Reference

The following table summarizes the properties for Queue Storage bindings:

Property Description Required
name The name of the variable in your function code. Yes
type queue (output) or queueTrigger (trigger). Yes
direction in for trigger, out for output. Yes
queueName The name of the queue to bind to. Yes
connection The name of the app setting containing the connection string. Defaults to AzureWebJobsStorage. No
accessRights For output bindings, specifies the access rights (e.g., Read, Write, Peek, Process). Defaults to Read, Write. No
newBatchSize The number of messages to retrieve from the queue in a single poll (trigger). Defaults to 16. No
isBatched Specifies if the trigger should process messages in batches. Defaults to true. No