Azure Functions Timer Trigger Tutorial

This tutorial guides you through creating and configuring an Azure Function that runs on a schedule using the Timer trigger.

What is an Azure Functions Timer Trigger?

The Timer trigger allows you to run code on a defined schedule. It's ideal for tasks like:

Prerequisites

Before you begin, ensure you have the following:

Step 1: Create a New Azure Functions Project

Using Azure Functions Core Tools

Open your terminal or command prompt and run the following commands:

bashfunc init MyTimerFunctionApp --worker-runtime csharp --docker

Replace csharp with your preferred runtime (e.g., node, python, powershell).

Navigate into the newly created directory:

bashcd MyTimerFunctionApp

Step 2: Add a Timer Trigger Function

Using Azure Functions Core Tools

Run the following command to create a new function with a Timer trigger:

bashfunc new --name MyTimerTrigger --template "Timer trigger"

This command creates a new folder named MyTimerTrigger containing your function files.

Step 3: Configure the Timer Schedule

The Timer trigger's schedule is defined in the function.json file within your trigger's folder. Open MyTimerTrigger/function.json.

json{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "mytimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */5 * * * *"
    }
  ]
}

The schedule property uses a CRON expression. In the example above, "0 */5 * * * *" means the function will run every 5 minutes.

Common CRON Expressions:

Important: For Azure Functions, the default CRON expression uses UTC time.

Step 4: Write Your Timer Trigger Code

The actual code for your timer function depends on your chosen runtime.

Example: Python

Open MyTimerTrigger/__init__.py and modify it as follows:

pythonimport datetime
import logging

import azure.functions as func

def main(mytimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().isoformat()

    if mytimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function executed at %s', utc_timestamp)

Example: C#

Open MyTimerTrigger.cs (or similar) and modify it.

csharpusing System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

namespace MyTimerFunctionApp
{
    public static class MyTimerTrigger
    {
        [FunctionName("MyTimerTrigger")]
        public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.UtcNow}");

            if (myTimer.IsPastDue)
            {
                log.LogInformation("C# Timer trigger function is running late!");
            }
        }
    }
}
Tip: You can use the IsPastDue property in your code to detect if the function missed a scheduled run.

Step 5: Run Your Function Locally

From the root of your project directory (MyTimerFunctionApp), run:

bashfunc start

Your function app will start, and the timer trigger will begin executing according to its schedule.

Step 6: Deploy to Azure

Once you are satisfied with your function, you can deploy it to Azure.

  1. Create an Azure Functions App resource in the Azure portal.
  2. Use the Azure Functions Core Tools to deploy:
    bashfunc azure functionapp publish <YourFunctionAppName>

Best Practices

This tutorial provided a basic introduction to creating an Azure Functions Timer Trigger. You can extend this by integrating with other Azure services or implementing more complex logic.