A time trigger automatically triggers a function at a specified point in time or at specified intervals. This allows you to flexibly schedule tasks in scenarios that require periodic task execution.
Scenarios
Time triggers can be used in a wide range of scenarios, such as:
Regular batch processing of data. For example, full data collection and report generation every hour.
Scheduling daily behaviors, such as sending coupons every hour on the hour.
Scheduling decoupled asynchronous tasks, such as deleting data at 00:00 every day.
Before you start
Step 1: Create a time trigger
Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
In the top navigation bar, select a region. On the Services page, find the service that you want to manage and click Functions in the Actions column.
On the Functions page, click the function that you want to manage.
On the function details page, click the Triggers tab, select a version or alias from the Version or Alias drop-down list, and then click Create Trigger.
In the Create Trigger panel, configure parameters and click OK. The following table describes the parameters.
Parameter
Description
Example
Trigger Type
Select Time Trigger.
Time Trigger
Parameter
Enter a trigger name.
timer-trigger
Version or Alias
The default value is LATEST. If you want to create a trigger for another version or alias, select a version or alias in the upper-right corner of the function details page. For more information about versions and aliases, see Manage versions and Manage aliases.
LATEST
Trigger Mode
Select a trigger mode. Options:
Interval: Enter a positive integer n in the Interval field. The function is triggered every n minutes.
Select Custom Time: Select a time zone and specify the dates, days of a week, and time. The function is triggered at the specified time.
Custom Settings: Specify a cron expression in the CRON Expression field. The function is triggered at the time specified by the cron expression.
Interval
Trigger Message
Enter custom parameters. The trigger message is used as the value of payload in event.
awesome-fc
You can configure the Cron Expression parameter to set the time zone. The following items describe the cron expression.
Cron expression (UTC time)
By default, the cron expression uses the Coordinated Universal Time (UTC) time. For example, if you want the function to be scheduled at 12:00 (UTC+8) every day, you can use the
0 0 4 * * *
expression.Cron expression (UTC+8)
If you want your function to be scheduled at specified points in time of a specific time zone, you can use CRON_TZ. For example, if you want to specify that the function to be invoked at 04:00 (UTC+8) on the first day of every month, you can use the
CRON_TZ=Asia/Shanghai 0 0 4 1 * *
expression. The time zone expression varies with the region.NoteIf daylight saving time (DST) and winter time are used in your time zone, a function is executed more frequently or less frequently when the time changes between the DST and the winter time. We recommend that you do not set the execution time to a time point when a change between DST and winter time occurs.
After the trigger is created, it is displayed on the Triggers tab. To modify or delete an existing trigger, see Manage triggers.
Step 2: Configure the input parameters of the function
On the function details page, click the Code tab, click the icon next to Test Function, and select Configure Test Parameters from the drop-down list.
In the Configure Test Parameters panel, click the Create New Test Event or Modify Existing Test Event tab, and specify Event Name and the event content. Then, click OK.
A time trigger uses the following event format to trigger a function:
{ "triggerTime":"2023-12-26T07:49:00Z", "triggerName":"timer-trigger", "payload":"awesome-fc" }
Parameter
Type
Example
Description
triggerTime
String
2023-12-26T07:49:00Z
The time when the function is triggered.
triggerName
String
timer-trigger
The name of the time trigger.
payload
String
awesome-fc
The value of Trigger Message that you entered when you created the trigger.
Step 3: Write and test the function
After you create the time trigger, you can write function code and test the function to verify whether the code is correct. When the specified time arrives, the time trigger automatically triggers the function.
On the function details page, click the Code tab, enter function code in the code editor, and then click Deploy.
Python code is used as an example in this topic.
import json import logging logger = logging.getLogger() def handler(event, context): logger.info('event: %s', event) # Parse the json evt = json.loads(event) triggerName = evt["triggerName"] triggerTime = evt["triggerTime"] payload = evt["payload"] logger.info('triggerName: %s', triggerName) logger.info("triggerTime: %s", triggerTime) logger.info("payload: %s", payload) return 'Timer Payload: ' + payload
Click the Code tab and click Test Function.
After the function is executed, you can view the results on the Code tab.
References
If the time trigger that you configured fails to trigger the associated function, check the trigger mode and the trigger time. If you set Trigger Mode to Custom Settings, take note that UTC time is used by default by the CRON Expression field. For more information about how to troubleshoot an execution failure, see the "Time triggers" item in Solution 2: Check whether the triggering rules are met.
If you have created an HTTP trigger for a function, you can no longer create a trigger of another type for the function. You can create another event function, configure the corresponding trigger, and configure access to the HTTP function in your code. For more information, see Use SDKs to run HTTP functions.
If you use a time trigger, the execution duration is calculated based on whether the function instance works in on-demand mode or provisioned mode. For more information about the calculation of execution durations, see Concepts.