Alibaba Cloud event source triggers refer to triggers of Alibaba Cloud services, such
as CloudMonitor triggers, Elastic Compute Service (ECS) triggers, and Alibaba Cloud
Internet of Things (IoT) triggers. This topic uses an ECS trigger as an example to
describe how to create an Alibaba Cloud event source trigger, write a function, and
test a function in the Function Compute console.
Overview
After you submit a request to create a trigger in the Function Compute console, Function Compute creates an event rule in the default event bus in the following format: Service name-Function name-Trigger name. After the trigger is created, you can view information about the trigger in the
Function Compute console. You can also view information about the created event rule
in the EventBridge console. When an event of the type specified by the event source is delivered to
the event bus, the function that is associated with the trigger is triggered to execute
once.
Note The name of the
EventBridge event rule must meet naming conventions. Otherwise, you cannot create the trigger. For more
information, see
Limits.
Usage notes
- You can create a maximum of 10 event rules for the system even bus named default in EventBridge. If the limit is exceeded, you cannot create Alibaba Cloud event source triggers.
- You cannot create Alibaba Cloud event source triggers by using Serverless Devs or
SDKs.
Prerequisites
- EventBridge
- Function Compute
Step 1: Create a 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 the service resides.
- On the Services page, find the service that you want to manage and click Functions in the Actions column.
- On the Functions page, click the name of 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, enter the relevant information. Click OK.

Parameter |
Description |
Example |
Trigger Type |
Select Elastic Compute Service from the drop-down list.
|
Elastic Compute Service |
Name |
The name of the trigger. Enter a custom trigger name. |
ecs-trigger |
Version or Alias |
The default value is LATEST. If you want to create a trigger of another version or alias, switch to the trigger
of the specified 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 |
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 of ECS.
|
Disk Retained |
Event Pattern Content |
After you set the Event Type parameter, the event pattern content is automatically filled. You cannot modify the
content. For more information about event patterns, see Event patterns.
|
{
"source": [
"acs.ecs"
],
"type": [
"ecs:Disk:ConvertToPostpaidCompleted"
]
}
|
Invocation Method |
The function invocation method. Valid values:
- Synchronous Invocation: After an event triggers the execution of a function, Function Compute returns the execution result when the execution is complete. This is the default
value. For more information, see Synchronous invocations.
- Asynchronous Invocation: After an event triggers the execution of a function, Function Compute immediately returns a response and ensures at least one successful execution of the
function. However, the detailed execution result is not returned. This invocation
method is applicable to a function that has relatively higher scheduling latency.
For more information, see Asynchronous invocations.
|
Synchronous Invocation |
After the trigger is created, the created trigger is displayed in the Trigger Name list.
Step 2: Write a function
After you create the trigger, you can write the function code.
On the function details page, click the Function Code tab and write code in the code editor.
The following sample code shows 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);
handle_event(event);
callback(null, 'return result');
}
Step 3: Test the function
After you write the function, you must debug the function to verify that the code
is correct. When the events that are generated by the ECS event sources are delivered
to Function Compute from EventBridge, function execution is automatically triggered.
An ECS event is passed to the function as an input parameter. You can manually pass
an event to the function to trigger the function and test whether the code of the
function is correct.
- On the Function Details page, click the Function Code tab, and then click the
icon. From the drop-down list, select Configure Test Parameters.
- In the Configure Test Parameters dialog box, click the Create New Test Event or Edit Existing Test Event tab, and specify the Event Name and event content. Click OK.
The event parameter is an input parameter of Function Compute. The following code
shows the format of the event parameter: For more information about the events of
Alibaba Cloud event sources, see Cloud service events.
{
"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.
Parameter |
Type |
Example |
Description |
datacontenttype |
String |
application/json;charset=utf-8 |
The content type of the data parameter. Only the application/json content type is supported.
|
aliyunaccountid |
String |
123456789098**** |
The ID of your Alibaba Cloud account. |
data |
Struct |
{
"result": "accomplished",
"diskId": "d-bp11ba7acc69nkta****"
}
|
The content of the event. The value is a JSON object, which is determined by the event
source that produces the event. CloudEvents may contain the context given by the event
producer when the event occurs. This information is encapsulated in the data parameter.
|
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 receives the event. |
id |
String |
70c0414c-b260-4923-b584-1d6e5646**** |
The ID of the event. |
time |
Timestamp |
2021-01-18T11:58:31.125+08:00 |
The time when the event occurred. |
aliyunregionid |
String |
cn-hangzhou |
The region in which the event was received. |
aliyunpublishaddr |
String |
172.25.XX.XX |
The IP address of the server that receives the event. |
- On the function details page, click the Function Code tab, and then click Test Function.
Check the result
On the Function Code tab, you can view a successful command.