Azure Timer Function: How to Force Immediate Execution with Manual Triggering

Azure Timer Function: How to Force Immediate Execution with Manual Triggering

Azure Functions is a serverless solution that allows you to write less code, maintain less infrastructure, and save on costs. Azure Functions provide a powerful way to execute code in response to various triggers, such as a file being uploaded to blob storage, an event pushed to EventHub, a call to a webhook, or a message added to Service Bus.

One of the most commonly used triggers is the Timer trigger. This trigger lets you run your Azure function on a predefined schedule. The schedule is defined via a CRON expression, and in some cases, the scheduled trigger is often hours apart. However, there could be scenarios where you need to run a timer function immediately rather than waiting for the scheduled time. Some of the most common examples include - a code bug that caused many executions to fail, and you want to clear the backlog immediately; or you made changes to the function and want to test immediately instead of waiting, or you have business scenarios that require you to sometimes trigger the timer function manually. One way to do this is to use the CRON expression from a configuration, update the configuration, and restart the Function. But that's clunky and inconvenient. In this article, we will see how to force immediate execution with manual triggering in an Azure Timer Function without changing the CRON expression.

Get the Function's Master Key

To trigger the Timer function manually, you need to get the Function's Master Key. To do this,

  1. Navigate to the function app in Azure Portal, select App Keys, and then select the _master key.

  2. Copy the key value as this would be used in the next step.

Make the HTTP request

To trigger the Timer function manually, make an HTTP POST request in the following form:
https://<function base-url>/admin/functions/<function-name>
with a header key of x-functions-key and the value of _master key copied earlier, and an empty JSON {} request body.

For example, if you have a function named TimerTrigger1 deployed in demoblogfunctions.azurewebsites.net you can trigger it manually with the following request:

POST 
https://demoblogfunctions.azurewebsites.net/admin/functions/TimerTrigger1

Request Headers:
x-functions-key: S5zxMCvMuDvQVegJEEmALUJEiLC1Rlj7Qz3QO_T3IB_vAzFuhlWa2w==
Content-Type: application/json

Request Body:
{}

If the request is sent successfully, you should receive an HTTP status code of 202 Accepted, and the timer function will be triggered immediately.

The function logs, as well as application insights if connected, will also indicate the trigger.

Caution: Due to the elevated permissions in your function app granted by the master key, you should not share this key with third parties or distribute it in an application. The key should only be sent to an HTTPS endpoint.

This approach to immediate execution using the admin APIs is not limited to Timer Trigger and can be used to run any non-HTTP-Triggered azure function by passing in the required input in the request body.