0
0
Azurecloud~5 mins

Functions with queue triggers in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want your app to do work only when new messages arrive in a queue. Azure Functions with queue triggers let your code run automatically when a message appears in an Azure Storage Queue. This helps you process tasks without checking the queue all the time.
When you want to process orders as soon as they are placed without delay.
When you need to resize images uploaded to storage automatically.
When you want to send emails after a user signs up, triggered by a queue message.
When you want to handle background tasks like data cleanup without blocking users.
When you want to scale processing automatically based on the number of messages.
Config File - function.json
function.json
{
  "bindings": [
    {
      "name": "myQueueItem",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "myqueue-items",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

This file tells Azure Functions to run the function when a new message arrives in the queue named myqueue-items. The myQueueItem is the variable that holds the message content. The connection points to the storage account connection string.

Commands
Create a new Azure Functions project using the .NET runtime to start building your function.
Terminal
func init MyQueueFunctionProj --worker-runtime dotnet
Expected OutputExpected
Writing C# project file to MyQueueFunctionProj.csproj Writing host.json Writing local.settings.json Writing .gitignore Function runtime initialized
--worker-runtime - Specifies the language runtime for the function app
Add a new function named QueueTriggerFunction that triggers when a message arrives in the queue.
Terminal
func new --name QueueTriggerFunction --template "Queue trigger"
Expected OutputExpected
Created new function 'QueueTriggerFunction' in project
--name - Sets the function name
--template - Chooses the function trigger template
Run the function app locally to test that it triggers when a message is added to the queue.
Terminal
func start
Expected OutputExpected
Hosting environment: Production Content root path: /path/to/MyQueueFunctionProj Now listening on: http://localhost:7071 Application started. Press Ctrl+C to shut down. [QueueTriggerFunction] C# Queue trigger function processed message: Hello from queue
Add a test message to the Azure Storage Queue to trigger the function.
Terminal
az storage message put --queue-name myqueue-items --content "Hello from queue" --account-name mystorageaccount --account-key XXXXXXXXXXXXXXXXXXXXXXXXX
Expected OutputExpected
{"popReceipt":"AgAAAAMAAAAAAAAA+V7v1w==","timeNextVisible":"2024-06-01T12:00:00Z","messageId":"12345678-1234-1234-1234-123456789abc"}
--queue-name - Specifies the target queue
--content - The message content to add
--account-name - Storage account name
--account-key - Storage account key for authentication
Key Concept

If you remember nothing else from this pattern, remember: Azure Functions with queue triggers run your code automatically whenever a new message arrives in the specified queue.

Common Mistakes
Using the wrong queue name in the function.json file.
The function will not trigger because it listens to a different queue than where messages are sent.
Ensure the queueName in function.json matches exactly the queue where messages are added.
Not setting the AzureWebJobsStorage connection string in local.settings.json or Azure portal.
The function cannot connect to the storage account to read queue messages, so it never triggers.
Set the AzureWebJobsStorage connection string correctly in local.settings.json for local testing or in the app settings in Azure.
Running the function app without starting the Azure Storage Emulator or having access to the storage account.
The function cannot listen to the queue, so no messages trigger the function.
Start the Azure Storage Emulator locally or ensure the function app has network access to the Azure Storage account.
Summary
Initialize a new Azure Functions project with the correct runtime.
Create a function with a queue trigger that listens to a specific Azure Storage Queue.
Run the function app locally and add messages to the queue to see the function trigger automatically.
Ensure connection strings and queue names match between your function and storage account.