Azure Functions Queue Storage Bindings

This article explains how to use Queue storage bindings in Azure Functions. Queue storage is a service that stores large numbers of small messages that can be accessed from anywhere in the world via HTTP/S. Azure Functions provides input and output bindings for Azure Queue Storage, allowing you to easily integrate with queues in your serverless applications.

Queue Storage bindings enable your functions to react to new messages in a queue or to add new messages to a queue.

Trigger: Queue Trigger

A Queue trigger starts your function when a new message is added to an Azure Storage Queue. The message content is passed to the function as a parameter.

Example: C# Queue Trigger


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

namespace MyCompany.MyFunctions
{
    public static class QueueTriggerFunction
    {
        [Function("QueueTriggerCSharp")]
        public static void Run(
            [QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem,
            FunctionContext context)
        {
            var logger = context.GetLogger();
            logger.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
        }
    }
}
            

Example: JavaScript Queue Trigger


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

Explanation:

Note: The default behavior of the Queue trigger is to delete the message from the queue after the function has successfully processed it.

Input Binding: Queue Input Binding

A Queue input binding allows you to read a specific message from an Azure Storage Queue. This is useful when you need to process a message within a function that is triggered by another event.

Example: C# Queue Input Binding


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

namespace MyCompany.MyFunctions
{
    public static class QueueInputBindingFunction
    {
        [Function("QueueInputBindingCSharp")]
        public static void Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
            [Queue("myinputqueue", Connection = "AzureWebJobsStorage")] string queueMessage,
            FunctionContext context)
        {
            var logger = context.GetLogger();
            logger.LogInformation($"HTTP trigger processed a request.");

            if (queueMessage != null)
            {
                logger.LogInformation($"Message from queue: {queueMessage}");
            }
            else
            {
                logger.LogInformation("No message found in the queue.");
            }
        }
    }
}
            

Output Binding: Queue Output Binding

A Queue output binding allows you to add a message to an Azure Storage Queue. This is commonly used to pass data to another function or system for further processing.

Example: C# Queue Output Binding


using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using System.Net;

namespace MyCompany.MyFunctions
{
    public static class QueueOutputBindingFunction
    {
        [Function("QueueOutputBindingCSharp")]
        public static async Task<HttpResponseData> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestData req,
            [Queue("myoutputqueue", Connection = "AzureWebJobsStorage")] out string queueMessage,
            FunctionContext context)
        {
            var logger = context.GetLogger();
            logger.LogInformation("HTTP trigger processed a request to send a message to a queue.");

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            queueMessage = $"Processed data: {requestBody}";

            var response = req.CreateResponse(HttpStatusCode.OK);
            response.WriteString("Message added to the queue successfully!");
            return response;
        }
    }
}
            
Tip: You can use the out keyword in C# to define a queue output binding.

Configuration

To use Queue Storage bindings, you need to configure your Azure Functions project:

  1. Ensure you have an Azure Storage account.
  2. Add the AzureWebJobsStorage application setting in your function app configuration to point to your storage account connection string.
  3. For queue names, you can directly specify them in the binding attribute (e.g., "myqueue-items").

Queue Message Properties

When working with Queue triggers and input bindings, the message content is typically passed as a string. For more complex scenarios, you can use JSON serialization/deserialization.

Warning: Be mindful of message size limits for Azure Queues (64 KB). For larger data, consider using Blob Storage and passing a reference to the blob in the queue message.

By leveraging these Queue Storage bindings, you can build powerful, event-driven applications with Azure Functions.