Azure Functions Storage Queue Bindings

This document describes how to use Azure Queue Storage bindings with Azure Functions. These bindings allow your functions to trigger on new messages in a queue or to output messages to a queue.

Triggering a Function from a Storage Queue

The Queue trigger starts your function when a new message is added to a specified Azure Queue Storage queue.

Attributes and Configuration

You can configure the Queue trigger using attributes in your function code (e.g., C#) or through the function.json file.

C# Attribute Example

using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace FunctionAppQueueTrigger
{
    public class QueueTriggerFunction
    {
        [Function("QueueTriggerFunction")]
        public void Run(
            [QueueTrigger("my-queue-name", Connection = "AzureWebJobsStorage")] string myQueueItem,
            FunctionContext context)
        {
            var logger = context.GetLogger();
            logger.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
        }
    }
}

function.json Example (Node.js)

{
  "scriptFile": "index.js",
  "bindings": [
    {
      "name": "myQueueItem",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "my-queue-name",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

Properties

Output Binding to a Storage Queue

The Queue output binding allows your function to add messages to a specified Azure Queue Storage queue.

Attributes and Configuration

C# Attribute Example

using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace FunctionAppQueueOutput
{
    public class QueueOutputFunction
    {
        [Function("QueueOutputFunction")]
        [QueueOutput("output-queue-name", Connection = "AzureWebJobsStorage")]
        public string Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
            FunctionContext context)
        {
            var logger = context.GetLogger();
            logger.LogInformation("HTTP trigger function processed a request.");
            string responseMessage = "Queue message sent!";
            return responseMessage;
        }
    }
}

function.json Example (Node.js)

{
  "scriptFile": "index.js",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "name": "outputQueueItem",
      "type": "queue",
      "direction": "out",
      "queueName": "output-queue-name",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

Properties

Supported Data Types

The Queue trigger and output bindings support various data types, including:

Important Considerations

When using Queue triggers, ensure your function is idempotent. Functions can be triggered multiple times for the same message if an error occurs after the message is dequeued but before it's successfully processed.

Connection String Management

Connection strings should be stored securely in application settings or Azure Key Vault. Never hardcode connection strings directly in your code.

Advanced Scenarios

Handling Poison Messages

Queue triggers have built-in handling for "poison messages" (messages that repeatedly cause function execution to fail). After a configurable number of retries, the message is moved to a special "poison queue" (named by appending -poison to the original queue name).

Batching

For higher throughput, you can configure Queue triggers to process messages in batches. This is supported for certain languages and runtime versions.

Queue Message Visibility Timeout

When a message is dequeued by a Queue trigger, its visibility is set to a timeout. If the function execution is not completed within this timeout, the message becomes visible again and can be dequeued by another instance. This timeout can be configured.