A time trigger automatically executes a function at a specific time or on a recurring schedule. This provides flexible task scheduling and is ideal for applications that require periodic tasks.
Scenarios
Common scenarios for time triggers include the following:
Scheduled batch data processing, such as collecting full data every hour to generate reports.
Scheduling daily actions, such as sending coupons on the hour.
Asynchronous tasks decoupled from business logic, such as cleaning up data at midnight 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, select the Trigger tab and then click Create Trigger.
In the Create Trigger panel, configure the parameters and click OK.
Parameter
Action
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. To create a trigger for another 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 Version management and Alias management.
LATEST
Trigger Mode
Select a trigger method as needed:
Interval: In the Interval text box, enter a positive integer n to execute the function every n minutes.
Select Custom Time: Select a time zone and specify a date, day of the week, and time to execute the function at the specified time in that time zone.
Custom Settings: In the Cron Expression text box, enter a cron expression to execute the function at the time specified by the expression.
Time Interval
Trigger Message
Enter custom parameters. This trigger message is used as the value of the payload in the event.
The size of the trigger message is limited to 128 KB, which is the same as the payload limit for asynchronous invocations.
awesome-fc
If you have specific time zone requirements and need to customize the Cron Expression, see the following descriptions of time expressions.
Cron expression (UTC)
Cron expressions run in UTC by default, which is 8 hours earlier than Beijing Time (UTC+8). For example, to schedule a function to run daily at 12:00 in Beijing Time (UTC+8), the equivalent UTC time is 04:00, so you can use
0 0 4 * * *.Cron expression (UTC+8)
If your task needs to run in a specific time zone, 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
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), tasks may be executed twice or skipped during the transition between DST and standard time. We recommend scheduling tasks outside of these transition periods.
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 entry 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 Modify Existing Test Event. Enter an event name and event content, and then click OK.
A time trigger uses the following event format to trigger the function.
{ "triggerTime":"2023-12-26T07:49:00Z", "triggerName":"timer-trigger", "payload":"awesome-fc" }Parameter
Type
Example Value
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 parameter that you entered in the trigger configuration. This is the value of the Trigger Message.
Step 3: Write and test the function code
After creating the time trigger, write and test the function code to verify that it works correctly. The time trigger automatically executes the function at the specified time.
On the function details page, in the Code tab, write the code in the code editor and then click Deploy.
This topic uses Python code as an example. The sample code is as follows.
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 execution completes, view the result above the Code tab.
References
If your configured time trigger fails to execute the function, first check the trigger method and the corresponding time settings. If you set a custom Cron Expression, note that it uses UTC by default. For troubleshooting methods, see What do I do if a trigger fails to execute a function?.