Event triggers let you invoke a Function Compute function automatically when an event occurs in another Alibaba Cloud service. Supported event sources include Cloud Monitor, audit events, Elastic Compute Service (ECS), Alibaba Cloud Internet of Things (IoT), and Operations and Maintenance (O&M) services.
This guide uses an ECS instance event as an example and walks you through creating a trigger, testing the event input, and writing the function handler.
How it works
When you create an event trigger in the Function Compute console, Function Compute automatically creates a corresponding event rule on the default event bus in EventBridge. The rule is named {FunctionName}-{TriggerName}. When an event matching the rule is delivered to the event bus, the associated function is invoked.
Limits
The default event bus supports a maximum of 10 event rules for Alibaba Cloud service event sources. Reaching this limit prevents you from creating additional event triggers for Alibaba Cloud services.
Serverless Devs cannot be used to create event triggers for Alibaba Cloud services.
Prerequisites
Before you begin, make sure you have:
Activated EventBridge and granted the required permissions. See Activate EventBridge and grant permissions.
Created a function in Function Compute. See 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 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 following parameters and click OK.
| Parameter | Description | Example |
|---|---|---|
| Trigger type | Select the event source service. | Elastic Compute Service (ECS) |
| Name | Enter a name for the trigger. | ecs-trigger |
| Version or alias | Defaults to LATEST. To target a specific version or alias, switch to it in the upper-right corner of the function details page before creating the trigger. See Manage versions and Manage aliases. | LATEST |
| Event type | Select Custom event types to pick specific ECS event types, or Select all event types to capture all ECS events. | Disk Retained |
| Event pattern content | Auto-populated based on the selected event type. Cannot be edited manually. The pattern mirrors the event structure — each field in the pattern matches the corresponding field in the actual event. For example, "source": ["acs.ecs"] in the pattern matches "source": "acs.ecs" in the event. See Event patterns. | {"source":["acs.ecs"],"type":["ecs:Disk:ConvertToPostpaidCompleted"]} |
| Invocation method | Synchronous invocation (default): the function runs and returns a result before EventBridge proceeds. Asynchronous invocation: EventBridge returns immediately after queuing the event; Function Compute guarantees at-least-once execution. Use asynchronous invocation for functions with long scheduling latencies. | Synchronous invocation |
| Trigger state | Enables or disables the trigger immediately after creation. Defaults to Enable trigger. | Enable trigger |
After the trigger is created, it appears on the Triggers tab. To modify or delete it, see Manage triggers.
Step 2: Configure test parameters
ECS events are delivered to your function as the event input parameter. Before a real event fires, configure a test event to verify that your function handles the payload correctly.
On the Code tab of the function details page, click the
icon next to 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 a name and the event content, then click OK.
The following is the structure of an ECS event payload:
{
"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 event follows the CloudEvents specification. The top-level fields such as source, type, id, and time are consistent across all Alibaba Cloud service event sources. The data field is event-specific — its content is determined by the service that generates the event. When writing your function handler, focus on parsing the data field for business logic; the other fields can be used for routing or logging.
| Field | Type | Example | Description |
|---|---|---|---|
| datacontenttype | String | application/json;charset=utf-8 | Content format of the data field. Supports application/json only. |
| aliyunaccountid | String | 123456789098**** | Your Alibaba Cloud account ID. |
| data | Struct | {"result":"accomplished","diskId":"d-bp11ba7acc69nkta****"} | Event-specific payload. Content varies by event source. |
| subject | String | acs:ecs:cn-hangzhou:123456789098**:disk/d-bp11ba7acc69nkta** | The resource that triggered the event. |
| source | String | acs.ecs | The event source service. |
| type | String | ecs:Disk:ConvertToPostpaidCompleted | The event type. |
| aliyunpublishtime | Timestamp | 2021-01-18T03:58:31.762Z | Time the event was received by EventBridge. |
| specversion | String | 1.0 | CloudEvents specification version. |
| aliyuneventbusname | String | default | Name of the event bus that received the event. |
| id | String | 70c0414c-b260-4923-b584-1d6e5646**** | Unique event ID. |
| time | Timestamp | 2021-01-18T11:58:31.125+08:00 | Time the event occurred. |
| aliyunregionid | String | cn-hangzhou | Region where the event was received. |
| aliyunpublishaddr | String | 172.25.XX.XX | IP address of the EventBridge server that received the event. |
For event payloads from all supported Alibaba Cloud service sources, see Alibaba Cloud service event sources.
Step 3: Write and test the function code
After creating the trigger, write the function handler to process incoming events. Parse the data field for event-specific information; the other top-level fields are consistent across event types.
On the function details page, click the Code tab. Write the function code in the editor, then click Deploy code. The following example shows how to handle an ECS event 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 to run the function with the test event configured in Step 2.
What's next
To modify or delete the trigger, see Manage triggers.