A time trigger automatically triggers a function at specified points 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. The following items list common scenarios:
Regular batch processing of data, for example, full data collection and generation of reports every hour
Scheduling of daily behaviors, such as sending coupons each hour at the top of the hour
Processing of asynchronous tasks that are decoupled from the business, for example, cleaning 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 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 Configurations tab. In the left-side navigation pane, click Triggers. Then, click Create Trigger.
In the Create Trigger panel, follow the on-screen instructions to configure parameters and click OK.
Parameter
Description
Example
Trigger Type
Select Time Trigger.
Time Trigger
Name
The name of the trigger.
my_trigger
Version or Alias
The version or alias of the trigger. Default value: 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. Valid values:
Interval: Enter a positive integer n in this field. The value indicates that the function is triggered every n minutes.
Select Custom Time: Select a time zone and specify the dates, days of a week, and time. If you select this value, the function is triggered at the specified time.
Custom Settings: Specify a cron expression in the CRON Expression field. If you select this value, 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 the event.
The size limit of a trigger message is 128 KB, which is the same as the limit on a payload of an asynchronous invocation.
awesome-fc
You can configure the Cron Expression parameter to specify the time zone. The following items describe cron expressions.
Cron expressions in UTC
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 in 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 trigger a function 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 based on 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 a trigger, see Manage triggers.
Step 2: Configure input parameters of the function
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, click the Create New Test Event or Modify Existing Test Event tab, enter the event name and event content, and then click OK.
A time trigger triggers a function based on the following event 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 is triggered.
triggerName
String
timer-trigger
The name of the time trigger.
payload
String
awesome-fc
The custom value that you specified for the Trigger Message parameter when you created the trigger.
Step 3: Write and test function code
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 is reached, 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.
The following sample code provides an example on how to write function code in Python:
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 Test Function.
After the function is executed, you can view the result 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 for the cron expression you specify in CRON Expression. For more information, see What do I do if a trigger cannot trigger function execution?
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.