Event triggers for Alibaba Cloud services allow you to trigger function execution 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 Service name-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 that is specified by the event source is delivered to the event bus, the function that is associated with the trigger is triggered to execute once.

Precautions

  • 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 begin

Step 1: Create a trigger

  1. Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
  2. In the top navigation bar, select a region. On the Services page, find the desired service and click Functions in the Actions column.
  3. 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.
  4. In the Create Trigger panel, specify related parameters. After you specify the parameters, click OK.
    Parameter Description Example
    Trigger Type Select Elastic Compute Service (ECS) from the drop-down list. Elastic Compute Service (ECS)
    Name Enter a custom 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 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 populated. 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 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. This invocation method is suitable for functions that have higher scheduling latency. For more information, see Overview.
    Synchronous Invocation

    After the trigger is created, it is displayed on the Triggers tab. To modify or delete an existing trigger, see Trigger management.

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.

  1. On the function details page, click the Code tab and click the xialatubiao icon. From the drop-down list that appears, select Configure Test Parameters.
  2. In the Configure Test Parameters panel, click the Create New Test Event or Modify Existing Test Event tab, and specify Event Name and the event content. After you specify the parameters, click OK.

    Sample code 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 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 source of the event.
    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.

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.

  1. On the function details page, click the Code tab, edit the function code in the code editor, and then click Deploy.

    This topic 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');
    }
  2. Click the Code tab and click Test Function.
    After the function is executed, you can view the result on the Code tab.

Additional information

To modify or delete an existing trigger, see Trigger management.