Event triggers for Alibaba Cloud services allow you to trigger functions using events from other Alibaba Cloud services. These events include Cloud Monitor events, audit events, Elastic Compute Service (ECS) events, Alibaba Cloud Internet of Things (IoT) events, and Operations and Maintenance (O&M) events from various Alibaba Cloud services. This topic uses an ECS instance as an example to describe how to create an event trigger for an Alibaba Cloud service, configure function input parameters, and write code in the Function Compute console.
Overview
When you submit a request to create a trigger in the Function Compute console, Function Compute automatically creates an event rule on the default event bus. The rule is named in the Function name-Trigger name format. After the trigger is created, you can view the trigger information in the Function Compute console. You can also view the automatically created event rule in the EventBridge console. When an event of the specified type is delivered from the event source to the event bus, the function associated with the trigger is executed.
Usage notes
You can create a maximum of 10 event rules on the default event bus for Alibaba Cloud services in EventBridge. If you reach the limit, you cannot create more event triggers for Alibaba Cloud services.
You cannot use Serverless Devs to create event 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
Procedure
Example
Trigger Type
Select Elastic Compute Service (ECS).
Elastic Compute Service (ECS)
Name
Enter a custom name for the trigger.
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
You cannot manually edit the content. After you select an event type for the Event Type parameter, the event pattern content is automatically populated. For more information about event patterns, see Event patterns.
{ "source": [ "acs.ecs" ], "type": [ "ecs:Disk:ConvertToPostpaidCompleted" ] }
Invocation Method
Select an invocation method for the function. Valid values:
Synchronous Invocation: This is the default invocation method. An event triggers a function. After the function execution is complete, Function Compute returns the execution result. For more information, see Synchronous invocations.
Asynchronous Invocation: This method is suitable for functions that have long scheduling latencies. After an event triggers 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 immediately after it is created. By default, Enable Trigger is selected. This means 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 Trigger management.
Step 2: Configure function input parameters
An ECS event is passed to the function as the event
input parameter. You can manually pass the event
to the function to simulate an event 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 Edit Existing Test Event, enter the event name and event content, and then click OK.
The following code shows the format of the
event
parameter. For more information about the event content for all supported Alibaba Cloud service event sources, 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 the event parameters.
Parameter
Type
Example
Description
datacontenttype
String
application/json;charset=utf-8
The content format of the data parameter. The datacontenttype parameter supports only the application/json format.
aliyunaccountid
String
123456789098****
The ID of your Alibaba Cloud account.
data
Struct
{ "result": "accomplished", "diskId": "d-bp11ba7acc69nkta****" }
The event content. The value is a JSON object. The content is determined by the service that generates the event. CloudEvents may contain context that is provided 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 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 protocol.
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 address of the server that receives the event.
Step 3: Write and test the function code
After you create the ECS trigger, you can write and test the function code to verify that it works correctly. In a production environment, when an event from the ECS event source is delivered to Function Compute through EventBridge, the trigger automatically executes the function.
On the function details page, click the Code tab. Write the code in the code editor and click Deploy Code.
The following code provides an example in Node.js.
'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.