A time trigger automatically runs a function at specified times or intervals. This feature provides flexible task scheduling for scenarios that require periodic execution.
Scenarios
Time triggers can be used in a wide range of scenarios. Common scenarios include the following:
Regular batch processing of data, such as collecting full data and generating reports every hour.
Scheduling daily actions, such as sending coupons at the beginning of each hour.
Running asynchronous tasks that are decoupled from your business logic, such as cleaning data at 00:00 every day.
Prerequisites
Step 1: Create a time trigger
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
On the function details page, click the Triggers tab and then click Create Trigger.
In the Create Trigger panel, configure the parameters and click OK.
Parameter
Procedure
Example
Trigger Type
Select Time Trigger.
Time Trigger
Name
Enter a custom name for the trigger.
my_trigger
Version Or Alias
The default value is LATEST. If you want to create a trigger for a different version or alias, first switch to that 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 Method
Select a trigger method as needed:
Interval: In the Interval text box, enter a positive integer n. The function is then triggered every n minutes.
Specified Time: Select a time zone and specify a date, day of the week, and time. The function is then triggered at the specified time in that time zone.
Custom: In the CRON Expression text box, enter a cron expression. The function is then triggered at the time specified by the cron expression.
Interval
Trigger Message
Enter a custom message. The trigger message is used as the value of the payload in the event.
The size of a trigger message is limited to 128 KB, which is the same as the payload limit for an asynchronous invocation.
awesome-fc
If you have specific time zone requirements and need to use a custom CRON Expression, refer to the following descriptions of time expressions.
Cron expression (UTC)
By default, a cron expression is based on Coordinated Universal Time (UTC). UTC is 8 hours behind the UTC+8 time zone. For example, to schedule a function to run at 12:00 (UTC+8) every day, you must convert this time to 04:00 (UTC). The corresponding cron expression is
0 0 4 * * *.Cron expression (UTC+8)
If your task needs to run in a specific time zone, you can specify it using CRON_TZ. For example, to trigger a function at 04:00 on the first day of each month in the UTC+8 time zone, use the expression
CRON_TZ=Asia/Shanghai 0 0 4 1 * *. Time zone expressions vary by region. Use the expression that applies to your region.NoteIf your time zone observes daylight saving time (DST), a function might be triggered twice or skipped during the transition. To avoid this, do not schedule tasks during the time when DST changes occur.
After the trigger is created, it is displayed on the Triggers tab. To modify or delete a trigger, see Trigger management.
Step 2: Configure the function's input parameters
On the Code tab of the function details page, click the
icon next Test Function and select Configure Test Parameters from the drop-down list. In the Configure Test Parameters panel, select Create New Test Event or Edit Existing Test Event, enter an event name and event content, and then click OK.
A time trigger invokes a function with an event that uses the following format.
{ "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 was triggered.
triggerName
String
timer-trigger
The name of the time trigger.
payload
String
awesome-fc
The custom message that you entered for the Trigger Message parameter in the trigger configuration.
Step 3: Write and test the function code
After you create the time trigger, write and test your function code to verify that it works correctly. The time trigger automatically runs the function at the specified time.
On the function details page, on the Code tab, write your code in the code editor and then click Deploy Code.
This topic uses Python as an example. The following sample code is for your reference.
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: ' + payloadClick Test Function.
After the execution is complete, you can view the result above the Code tab.
References
If the time trigger that you configured fails to trigger the associated function, check the trigger mode and time. If you set Trigger Mode to Custom Settings, note that the cron expression you specify in CRON Expression uses UTC. For more information, see What do I do if a trigger cannot trigger function execution?.