Azure Functions Timer Trigger

The Timer trigger allows you to run Azure Functions on a schedule. It uses a cron-like expression to define the schedule.

What is a Timer Trigger?

A Timer trigger is a type of Azure Function binding that enables your function to execute at regular intervals. This is incredibly useful for tasks like:

Configuration

The Timer trigger is configured in the function.json file for your function.

Type: timerTrigger
Direction: in
Name: The name of the parameter in your function code that will receive the timer event (e.g., myTimer).
Schedule: A cron expression that defines when the function should run. This can also be a simple predefined schedule like @hourly, @daily, @weekly, @monthly.

Example function.json

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

In this example, "schedule": "0 */5 * * * *" means the function will run every 5 minutes.

Cron Expression Format

The cron expression follows a 6-field format:

{second} {minute} {hour} {day of month} {month} {day of week}

For example:

You can find detailed information on cron expressions and Azure Functions scheduling here.

Accessing Timer Information in Code

The Timer trigger provides information about the elapsed time and the next scheduled execution time.

Python Example (__init__.py)

import 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)
    logging.info('Next timer fire at: %s', myTimer.next_fire_time)

C# Example (TimerTrigger.cs)

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

public static class TimerTrigger
{
    [FunctionName("TimerTriggerCSharp")]
    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: Timer is past due!");
        }

        log.LogInformation($"Next timer to fire at: {myTimer.ScheduleStatus.Next}");
    }
}

Key Features