This topic describes how to configure a time trigger for a function in the Function Compute console, including how to create a time trigger, configure input parameters, write
function code, and test the function.
Step 1: Create a time trigger
- Log on to the Function Compute console.
- In the left-side navigation pane, click Services and Functions.
- In the top navigation bar, select the region where your Kubernetes cluster is deployed.
- On the Services page, find the desired service 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 the version or alias from the Version or Alias drop-down list, and then click Create Trigger.
- In the Create Trigger panel, specify related parameters. After you specify the parameters,
click OK.
Parameter |
Description |
Example |
Trigger Type |
Select Time Trigger.
|
Time Trigger |
Parameter |
The name of the trigger. Enter a custom trigger name. |
my_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 of a service, see Manage versions and Manage aliases.
|
LATEST |
Trigger Mode |
Select a trigger mode:
- 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: 5 minutes |
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 to schedule a function at 12:00 (UTC+8) every day, you must use
0 0 4 * * *
, which specifies that the function is scheduled at 04:00 (UTC) every day.
- Cron expression (UTC+8)
If you want to trigger a function based on a specific time zone, you can use the CRON_TZ expression. For example, if you want to trigger the function at 04:00 on the first
day of every month (UTC+8), you can use CRON_TZ=Asia/Shanghai 0 0 4 1 * *
. The time zone expression varies with the region.
Note
If 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.
Step 2: Configure the input parameters of the function
- On the function details page, click the Code tab and click the
icon. From the drop-down list that appears, select Configure Test Parameters.
- 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. After you specify the parameters, click OK.
A time trigger uses the following event format to trigger a function:
{
"triggerTime":"2018-02-09T05:49:00Z",
"triggerName":"timer-trigger",
"payload":"awesome-fc"
}
Parameter |
Type |
Example |
Description |
triggerTime |
String |
2018-02-09T05: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 the function code 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 Save.
This example uses the Python function code.
import json
import logging
logger = logging.getLogger()
def handler(event, context):
logger.info('event: %s', event)
evt = json.loads(event)
triggerName = evt["triggerName"]
triggerTime = evt["triggerTime"]
message = evt["payload"]
creds = context.credentials
logger.info('access_key_id: %s', creds.access_key_id)
logger.info("message = %s", message)
- On the function details page, click the Code tab and click Test Function.
After the function is executed, you can view the result on the Code tab.
Additional information
In addition to the Function Compute console, you can configure triggers by using the
following methods:
- Use Serverless Devs to configure triggers. For more information, see Serverless Devs.
- Use SDKs to configure triggers. For more information, see Supported SDKs.
To modify or delete an existing trigger, see Manage triggers.