A trigger for an Alibaba Cloud service invokes a function in response to events from Alibaba Cloud services, such as Cloud Monitor, ActionTrail, ECS, and IoT Platform. This topic uses an ECS event as an example to show you how to create a trigger, configure its input parameter, and write function code on the Function Compute console.
How it works
When you create a trigger on the Function Compute console, Function Compute automatically creates an event rule named FunctionName-TriggerName on the default event bus in EventBridge. After the trigger is created, you can view its information in the Function Compute console and view the automatically created event rule on the EventBridge console. When an event of a specified type from the event source is delivered to the event bus, the function associated with the trigger is invoked.
Usage notes
You can create a maximum of 10 event rules on the default event bus in EventBridge. If this limit is reached, you cannot create more triggers for Alibaba Cloud services.
You cannot use Serverless Devs to create triggers for Alibaba Cloud services.
Prerequisites
EventBridge: Activate EventBridge and grant permissions
Function Compute: Create a function
Step 1: Create a 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, configure the parameters and click OK.
Parameter
Actions
Example
Trigger Type
Select ECS.
ECS
Name
Enter a custom name for the trigger.
ecs-trigger
Version or Alias
The default value is LATEST. To create a trigger for a different version or alias, first switch to it in the upper-right corner of the function details page. For more information, see Manage versions and Manage aliases.
LATEST
Event Type
Select Custom Event Types or Select All Event Types. If you select Custom Event Types, you can select one or more event types for ECS.
Disk Retained
Event Pattern Content
This parameter is read-only. The content is automatically populated after you select an event type. For more information about event patterns, see Event patterns.
{ "source": [ "acs.ecs" ], "type": [ "ecs:Disk:ConvertToPostpaidCompleted" ] }Invocation Method
Select the function invocation method. Valid values:
Sync Invocation: This is the default method. After an event triggers a function invocation, Function Compute waits for the invocation to complete and then returns the result. For more information, see synchronous invocation.
Async Invocation: This method is suitable for functions with long latencies. After an event triggers a function invocation, Function Compute immediately returns a response and ensures that the function executes successfully at least once but does not return the execution result. For more information, see asynchronous invocation.
Synchronous Invocation
Trigger State
Specifies if the trigger is enabled upon creation. By default, Enable Trigger is selected.
Enable Trigger
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 input parameter
The function receives ECS events as the event input parameter. You can manually pass a sample event to the function to simulate a trigger.
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 the event content, and click OK.
The following code shows the format of the
eventparameter. For the event content of all official event sources, see Alibaba Cloud official event sources.{ "datacontenttype": "application/json;charset=utf-8", "aliyunaccountid": "123456789098****", "data": { "result": "accomplished", "diskId": "d-bp11ba7acc69nkta****" }, "subject": "acs:ecs:cn-hangzhou:123456789098****:disk/d-bp11ba7acc69nkta****", "source": "acs.ecs", "type": "ecs:Disk:ConvertToPostpaidCompleted", "aliyunpublishtime": "2021-01-18T03:58:31.762Z", "specversion": "1.0", "aliyuneventbusname": "default", "id": "70c0414c-b260-4923-b584-1d6e5646****", "time": "2021-01-18T11:58:31.125+08:00", "aliyunregionid": "cn-hangzhou", "aliyunpublishaddr": "172.25.XX.XX" }The following table describes the parameters in the event payload.
Parameter
Type
Example
Description
datacontenttype
String
application/json;charset=utf-8
Specifies the content format of the data parameter. The datacontenttype parameter supports only the application/json format.
aliyunaccountid
String
123456789098****
The Alibaba Cloud account ID.
data
Struct
{ "result": "accomplished", "diskId": "d-bp11ba7acc69nkta****" }The event content. It is a JSON object whose content is determined by the service that initiated the event. CloudEvents may contain context from the event producer, which is encapsulated in the data field.
subject
String
acs:ecs:cn-hangzhou:123456789098****:disk/d-bp11ba7acc69nkta****
The subject of the event.
source
String
acs.ecs
The event source.
type
String
ecs:Disk:ConvertToPostpaidCompleted
The type of the event.
aliyunpublishtime
Timestamp
2021-01-18T03:58:31.762Z
The time when the event was received.
specversion
String
1.0
The version of the CloudEvents specification.
aliyuneventbusname
String
default
The name of the event bus that received the event.
id
String
70c0414c-b260-4923-b584-1d6e5646****
The event ID.
time
Timestamp
2021-01-18T11:58:31.125+08:00
The time when the event occurred.
aliyunregionid
String
cn-hangzhou
The region where the event was received.
aliyunpublishaddr
String
172.25.XX.XX
The IP address of the server that received the event.
Step 3: Write and test code
After creating the ECS trigger, you can write and test the function code to verify that it works as expected. In a real-world scenario, when an event from ECS is delivered to Function Compute through EventBridge, the trigger automatically invokes the function.
On the function details page, on the Code tab, write your code in the code editor and click Deploy.
This topic uses Node.js code as an example.
'use strict'; /* To enable the initializer feature please implement the initializer function as below: exports.initializer = (context, callback) => { console.log('initializing'); callback(null, ''); }; */ exports.handler = (event, context, callback) => { console.log("event: %s", event); // Parse the event parameters and process the event. callback(null, 'return result'); }Click Test Function.
More information
To modify or delete an existing trigger, see Manage triggers.