Azure Functions with Azure Storage Queues

This article explains how to integrate Azure Functions with Azure Storage Queues to build event-driven applications. Storage Queues provide a simple, scalable messaging solution for decoupling application components.

Introduction to Azure Storage Queues

Azure Storage Queues are a service for storing large numbers of messages that can be accessed from anywhere in the world via authenticated HTTP calls. A queue belongs to a storage account. A queue can contain an unlimited number of messages. However, there is a limit to the total size of messages that can be contained in the queue.

Using the Queue Trigger

The Queue trigger allows your function to run in response to a message appearing in a Storage Queue. When a message is received, the trigger invokes your function.

Example: Queue Trigger in C#

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

public static class QueueTriggerFunction
{
    [FunctionName("QueueTriggerCSharp")]
    public static void Run(
        [QueueTrigger("myqueue-items")] string myQueueItem,
        ILogger log)
    {
        log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
    }
}

Example: Queue Trigger in JavaScript

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

Using the Queue Output Binding

The Queue output binding allows your function to send messages to an Azure Storage Queue. This is useful for sending work items to be processed by other functions or services.

Example: Queue Output Binding in C#

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

public static class QueueOutputFunction
{
    [FunctionName("QueueOutputCSharp")]
    public static void Run(
        [TimerTrigger("0 */5 * * * *")] TimerInfo myTimer,
        [Queue("outputqueue", Connection = "AzureWebJobsStorage")] out string message,
        ILogger log)
    {
        message = $"New message generated at: {myTimer.ScheduleStatus.Next}";
        log.LogInformation($"C# function sent a message to the output queue: {message}");
    }
}

Example: Queue Output Binding in JavaScript

JavaScript
module.exports = async function (context, timer) {
    const timeStamp = new Date().toISOString();
    context.log('JavaScript is running function at timestamp:', timeStamp);

    context.bindings.message = `New message generated at: ${timeStamp}`;
    context.log(`JavaScript function sent a message to the output queue: ${context.bindings.message}`);
};
Important: Ensure your local.settings.json or application settings in Azure have the correct connection string for your storage account, typically named AzureWebJobsStorage.

Best Practices

Azure Functions and Storage Queues Architecture Diagram

Conceptual diagram showing Azure Functions triggering on Storage Queue messages.

Further Reading