This tutorial guides you through creating an Azure Function that uses a queue binding to process messages from an Azure Storage Queue. We'll focus on a JavaScript implementation.
F1
to open the Command Palette.Azure Functions: Create New Project...
.JavaScript
.Azure Queue Storage trigger
.QueueTriggerJS
).AzureWebJobsStorage
).myqueue-items
).local.settings.json
file.
Visual Studio Code will create a project structure that typically includes:
.gitignore
local.settings.json
: Stores app settings, including connection strings.package.json
: Manages project dependencies.QueueTriggerJS/index.js
: The JavaScript code for your function.QueueTriggerJS/function.json
: Configuration for your function, defining triggers and bindings.function.json
ExampleThis file defines the input and output bindings for your function. The queue trigger binding is specified here.
{
"bindings": [
{
"name": "myQueueItem",
"type": "queueTrigger",
"direction": "in",
"queueName": "myqueue-items",
"connection": "AzureWebJobsStorage"
}
],
"scriptFile": "index.js"
}
index.js
ExampleThis is where your JavaScript code resides. It receives messages from the queue.
module.exports = async function (context, myQueueItem) {
context.log('JavaScript queue trigger function processed work item:', myQueueItem);
// 'myQueueItem' contains the message from the queue.
// It can be a string, JSON object, or other data types depending on what was put into the queue.
// Example: If the queue item is a JSON object
if (typeof myQueueItem === 'object' && myQueueItem !== null) {
context.log('Processing JSON message:', myQueueItem);
if (myQueueItem.name) {
context.log('Hello, ' + myQueueItem.name + '!');
}
} else {
context.log('Processing string message:', myQueueItem);
}
// You can add further processing here, like writing to a database,
// sending an email, or triggering another service.
};
context.log
function is used for logging messages that appear in the Functions host logs.
local.settings.json
.Ctrl+` or Cmd+`).
func start
The Functions host will start, and your queue trigger function will be active, listening for messages on the specified queue.
You can add messages to the queue using various methods:
az storage message put --content "Hello from CLI" --queue-name myqueue-items --account-name
When a message is added to the myqueue-items
queue, your JavaScript Azure Function will automatically trigger and process it.
Once you're ready to deploy:
Deploy to Function App...
.After deployment, your function will run in Azure and process messages from your Azure Storage Queue.
You can also add output bindings to send data to other Azure services. For example, to write to another queue or a table:
{
"bindings": [
{
"name": "myQueueItem",
"type": "queueTrigger",
"direction": "in",
"queueName": "myqueue-items",
"connection": "AzureWebJobsStorage"
},
{
"name": "outputQueueItem",
"type": "queue",
"direction": "out",
"queueName": "processed-items",
"connection": "AzureWebJobsStorage"
}
],
"scriptFile": "index.js"
}
And in your index.js
:
module.exports = async function (context, myQueueItem) {
context.log('JavaScript queue trigger function processed work item:', myQueueItem);
// Process the message
const processedMessage = `Processed: ${myQueueItem}`;
// Set the output binding to send to another queue
context.bindings.outputQueueItem = processedMessage;
context.log(`Sent "${processedMessage}" to processed-items queue.`);
};
Azure Functions automatically retries queue-triggered functions on exceptions. You can configure retry policies in your host.json
file.
When sending JSON data to the queue, ensure your function correctly parses it. JavaScript Azure Functions often automatically deserialize JSON messages if the content type is appropriate.