Azure Functions Timer Triggers

Learn how to schedule code execution with Timer Triggers.

Introduction to 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.

Creating a Timer Trigger Function

Let's walk through the steps to create a simple Timer Trigger function using Node.js.

Step 1: Set up your Azure Functions Project

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

Step 2: Create a Timer Trigger Function

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:

Step 3: Configure the Timer Schedule

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:

For more advanced scheduling, consult the NCRONTAB expression documentation.

Step 4: Write Your Function Logic

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.

Step 5: Run and Deploy

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).

Key Concepts

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