All Products
Search
Document Center

Function Compute:Configure event triggers for Alibaba Cloud services

Last Updated:Apr 01, 2026

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:

Step 1: Create a trigger

  1. Log on to the Function Compute console. In the left-side navigation pane, click Functions.

  2. In the top navigation bar, select a region. On the Functions page, click the function you want to manage.

  3. On the function details page, click the Configurations tab. In the left-side navigation pane, click Triggers, then click Create Trigger.

  4. In the Create Trigger panel, configure the following parameters and click OK.

ParameterDescriptionExample
Trigger typeSelect the event source service.Elastic Compute Service (ECS)
NameEnter a name for the trigger.ecs-trigger
Version or aliasDefaults 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 typeSelect Custom event types to pick specific ECS event types, or Select all event types to capture all ECS events.Disk Retained
Event pattern contentAuto-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 methodSynchronous 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 stateEnables 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.

  1. On the Code tab of the function details page, click the image.png icon next to Test Function and select Configure test parameters from the drop-down list.

  2. 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.

FieldTypeExampleDescription
datacontenttypeStringapplication/json;charset=utf-8Content format of the data field. Supports application/json only.
aliyunaccountidString123456789098****Your Alibaba Cloud account ID.
dataStruct{"result":"accomplished","diskId":"d-bp11ba7acc69nkta****"}Event-specific payload. Content varies by event source.
subjectStringacs:ecs:cn-hangzhou:123456789098**:disk/d-bp11ba7acc69nkta**The resource that triggered the event.
sourceStringacs.ecsThe event source service.
typeStringecs:Disk:ConvertToPostpaidCompletedThe event type.
aliyunpublishtimeTimestamp2021-01-18T03:58:31.762ZTime the event was received by EventBridge.
specversionString1.0CloudEvents specification version.
aliyuneventbusnameStringdefaultName of the event bus that received the event.
idString70c0414c-b260-4923-b584-1d6e5646****Unique event ID.
timeTimestamp2021-01-18T11:58:31.125+08:00Time the event occurred.
aliyunregionidStringcn-hangzhouRegion where the event was received.
aliyunpublishaddrString172.25.XX.XXIP 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.

  1. 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');
    }
  2. Click Test Function to run the function with the test event configured in Step 2.

What's next