Event triggers for Alibaba Cloud services allow you to trigger functions by using events from Alibaba Cloud services. These events include CloudMonitor events, audit events, Elastic Compute Service (ECS) events, Alibaba Cloud IoT events, and O&M events from specific cloud services. This topic describes how to create an ECS trigger, configure function input parameters, and write and test code in the Function Compute console.
Overview
After you submit a request to create a trigger in the Function Compute console, Function Compute automatically creates an event rule that is named in the Function name-Trigger name format in the system event bus default. After the trigger is created, you can view the information about the trigger in the Function Compute console. You can also view the 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.
Usage notes
You can create up to 10 event rules in the system even bus default in EventBridge. If the upper limit is reached, you can no longer create event triggers for Alibaba Cloud services.
You cannot use Serverless Devs to create event triggers for Alibaba Cloud services.
Before you start
EventBridge: Activate EventBridge and grant permissions to a RAM user
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 parameters and click OK.
Parameter
Description
Example
Trigger Type
Select Elastic Compute Service (ECS) from the drop-down list.
Elastic Compute Service (ECS)
Name
Enter a trigger name.
ecs-trigger
Version or Alias
The default value is LATEST. If you want to create a trigger for another version or alias, switch to the specified 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
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 populated. You cannot modify the content. For more information about event patterns, see Event patterns.
{ "source": [ "acs.ecs" ], "type": [ "ecs:Disk:ConvertToPostpaidCompleted" ] }
Invocation Method
Specify the function invocation method. Valid values:
Synchronous Invocation: After an event triggers the execution of a function, Function Compute returns the 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 that the function is successfully executed at least once. However, the detailed execution result is not returned. For more information, see Overview.
Synchronous Invocation
Trigger State
Specifies whether to enable the trigger after it is created. By default, Enable Trigger is selected and the trigger is enabled after it is created.
Enable Trigger
After the trigger is created, it is displayed on the Triggers tab. To modify or delete a trigger, see Manage triggers.
Step 2: Configure the input parameters of the function
The ECS event source is passed to the function in the form of event
, which acts as an input parameter. You can manually pass event
to the function to trigger 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.
The following sample code provides an example of
event
. For more information about the supported events of Alibaba Cloud services, see Alibaba Cloud service 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 parameters that are contained in 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 the Alibaba Cloud account.
data
Struct
{ "result": "accomplished", "diskId": "d-bp11ba7acc69nkta****" }
The event content. 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 event subject.
source
String
acs.ecs
The event source.
type
String
ecs:Disk:ConvertToPostpaidCompleted
The event type.
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 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 receives the event.
Step 3: Write and test the function
After you create the ECS trigger, you can write function code and test the function to verify whether 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.
On the function details page, click the Code tab, enter function code in the code editor, and then click Deploy.
The following uses the Node.js function 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.
References
To modify or delete an existing trigger, see Manage triggers.