Learn how to schedule code execution with Timer Triggers.
Azure Functions Timer Triggers allow you to run code on a scheduled basis. This is incredibly useful for tasks like:
Timer Triggers use the NCRONTAB expression format for defining schedules, offering powerful flexibility.
Let's walk through the steps to create a simple Timer Trigger function using Node.js.
If you haven't already, set up your local development environment for Azure Functions. You can use the Azure Functions Core Tools.
To create a new project:
func init MyTimerProject --worker-runtime node --language javascript
Navigate into your project directory:
cd MyTimerProject
Use the Azure Functions Core Tools to generate a new Timer Trigger function:
func new --template "Timer trigger" --name MyTimerFunction
This will create a new folder named MyTimerFunction
containing your function files:
index.js
: The main code for your function.function.json
: The configuration file for your function, including bindings.Open the function.json
file within your MyTimerFunction
folder. You'll see a configuration for the timer trigger.
The key property is schedule
. This uses the NCRONTAB format. Here's an example that runs every 5 minutes:
{
"bindings": [
{
"name": "myTimer",
"type": "timerTrigger",
"direction": "in",
"schedule": "0 */5 * * * *"
},
{
"name": "outputQueueItem",
"type": "queue",
"direction": "out",
"queueName": "items",
"connection": "AzureWebJobsStorage"
}
],
"disabled": false
}
Common Schedule Examples:
"* * * * * *"
"0 */5 * * * *"
"0 15 * * * *"
"0 0 0 * * *"
For more advanced scheduling, consult the NCRONTAB expression documentation.
Open the index.js
file. This is where you'll write the code that executes on the schedule.
Here's a sample index.js
that logs a message and the current time:
module.exports = async function (context, myTimer) {
const timeStamp = new Date().toISOString();
if (myTimer.isPastDue) {
context.log('JavaScript timer trigger function is running late!');
}
context.log('JavaScript timer trigger function executed!', timeStamp);
// You can add your scheduled task logic here
// For example, interacting with other Azure services,
// processing data, sending notifications, etc.
// Example: Sending a message to a queue
// context.bindings.outputQueueItem = `Timer executed at: ${timeStamp}`;
};
The context
object provides logging capabilities and access to bindings. The myTimer
object contains information about the timer execution, including whether it's running late.
To run your function locally, use the Azure Functions Core Tools:
func start
Your Timer Trigger function will now execute according to the schedule defined in function.json
.
To deploy your function to Azure, you can use the Azure CLI:
az functionapp deployment source config-zip -g -n --src-zip .
Ensure your local.settings.json
(for local testing) or your Function App's Application Settings in Azure have the necessary connection strings (e.g., AzureWebJobsStorage
).
function.json
: Defines the trigger type, direction, and schedule.index.js
: Contains the code that runs when the timer elapses.Timer Triggers are a fundamental building block for creating automated workflows with Azure Functions. Experiment with different schedules and integrate them with other Azure services to build powerful serverless solutions.
Read Official Documentation