AZ Functions Timer Trigger and Scheduling
The Timer trigger runs an Azure Function on a fixed schedule — like a digital alarm clock that wakes the function at a specific time or interval. No one needs to call the function manually. Azure starts it automatically based on the schedule defined in the configuration.
Timer Trigger Concept – Analogy
┌──────────────────────────────────────────────────────────────┐ │ TIMER TRIGGER ANALOGY │ │ │ │ Traditional Alarm Clock: │ │ Set time → Clock reaches that time → Alarm rings │ │ │ │ Azure Timer Trigger: │ │ Set CRON schedule → Azure reaches that time → Function runs│ │ │ │ No one presses a button. No HTTP call. No manual action. │ │ Azure fires the function automatically. │ └──────────────────────────────────────────────────────────────┘
Common Use Cases
- Send a daily summary email every morning at 8:00 AM
- Clean up expired records in a database every Sunday at midnight
- Sync data between two systems every 15 minutes
- Generate monthly invoices on the first day of each month
- Check a third-party API for updates every 5 minutes
Timer Trigger Configuration
function.json
{
"bindings": [
{
"type": "timerTrigger",
"direction": "in",
"name": "myTimer",
"schedule": "0 0 8 * * *"
}
]
}
Key Properties
| Property | Purpose |
|---|---|
| type | Must be "timerTrigger" |
| direction | Always "in" for a trigger |
| name | Variable name used in function code to access timer info |
| schedule | CRON expression defining when the function runs |
| runOnStartup | If true, function also runs when the app starts (optional) |
CRON Expression Deep Dive
Azure Functions uses a 6-field CRON expression. Each field controls a time unit:
Position: 1 2 3 4 5 6
│ │ │ │ │ │
▼ ▼ ▼ ▼ ▼ ▼
Format: {second} {minute} {hour} {day/month} {month} {day/week}
Range: 0-59 0-59 0-23 1-31 1-12 0-6
(0=Sunday)
Special Characters in CRON
| Character | Meaning | Example |
|---|---|---|
| * | Every possible value | * in month = every month |
| , | List of specific values | 1,3,5 in day/week = Mon, Wed, Fri |
| - | Range of values | 1-5 in day/week = Mon through Fri |
| / | Step / interval | */5 in minute = every 5 minutes |
Practical CRON Examples
| CRON Expression | Schedule |
|---|---|
0 0 8 * * * | Every day at 8:00:00 AM |
0 30 9 * * 1 | Every Monday at 9:30 AM |
0 0 0 * * * | Every day at midnight |
0 */15 * * * * | Every 15 minutes |
0 0 0 1 * * | First day of every month at midnight |
0 0 9 * * 1-5 | Monday to Friday at 9:00 AM (weekdays only) |
0 0 */2 * * * | Every 2 hours |
0 0 9,17 * * * | Every day at 9:00 AM and 5:00 PM |
Timer Trigger Function Code
index.js
module.exports = async function (context, myTimer) {
// Check if the timer fired late (past the scheduled time)
if (myTimer.isPastDue) {
context.log("Timer is running late!");
}
// Log the current time
const now = new Date().toISOString();
context.log(`Daily report function ran at: ${now}`);
// Place business logic here
// Example: Send email, query database, clean up records
await sendDailyReport();
};
async function sendDailyReport() {
// Simulate sending a report
console.log("Daily report sent successfully.");
}
The TimerInfo Object
The timer trigger passes a TimerInfo object into the function. This object provides information about the schedule run.
| Property | Type | Description |
|---|---|---|
| isPastDue | boolean | True if the function ran later than its scheduled time |
| schedule.adjustForDST | boolean | Whether the schedule adjusts for Daylight Saving Time |
| scheduleStatus.last | DateTime | The last time this function ran |
| scheduleStatus.next | DateTime | The next scheduled time this function will run |
| scheduleStatus.lastUpdated | DateTime | When the schedule status was last updated |
Time Zones for Timer Triggers
By default, timer triggers use UTC time. This is important because 9:00 AM UTC is different from 9:00 AM IST (India Standard Time, UTC+5:30).
To use a specific time zone, add the WEBSITE_TIME_ZONE setting to the function app's Application Settings:
Key: WEBSITE_TIME_ZONE Value: India Standard Time
With this setting, the CRON expression 0 0 9 * * * runs at 9:00 AM IST instead of 9:00 AM UTC.
Common Time Zone Values
| Time Zone | Value for WEBSITE_TIME_ZONE |
|---|---|
| India Standard Time | India Standard Time |
| US Eastern | Eastern Standard Time |
| US Pacific | Pacific Standard Time |
| UK | GMT Standard Time |
| Singapore | Singapore Standard Time |
Practical Example – Weekly Database Cleanup
// Runs every Sunday at 1:00 AM
// CRON: "0 0 1 * * 0"
module.exports = async function (context, myTimer) {
context.log("Weekly cleanup started...");
// Simulate deleting records older than 90 days
const deletedCount = await deleteOldRecords(90);
context.log(`Cleanup complete. Deleted ${deletedCount} old records.`);
};
async function deleteOldRecords(daysOld) {
// In real code: connect to database and delete old records
return 42; // Simulate 42 records deleted
}
runOnStartup – Useful During Development
Adding "runOnStartup": true to function.json makes the timer function run immediately when the app starts. This is helpful during development to test the logic without waiting for the schedule to fire.
{
"bindings": [
{
"type": "timerTrigger",
"direction": "in",
"name": "myTimer",
"schedule": "0 0 8 * * *",
"runOnStartup": true
}
]
}
Important: Remove or set runOnStartup to false before deploying to production. Otherwise the function runs on every app restart, which may cause unintended side effects.
Timer Trigger Flow Summary
┌──────────────────────────────────────────────────────────────┐ │ TIMER TRIGGER FLOW │ │ │ │ Azure Internal Scheduler │ │ │ │ │ │ Checks: Is it time to run? (matches CRON schedule) │ │ │ │ │ ├── Not yet → Keep waiting │ │ │ │ │ └── Yes → Fire the timer trigger │ │ │ │ │ ▼ │ │ Function code runs │ │ │ │ │ ▼ │ │ Function completes → Sleep until next run │ └──────────────────────────────────────────────────────────────┘
