Event triggers for Alibaba Cloud services can be used to trigger the function execution by using Alibaba Cloud service events. These events include CloudMonitor events, audit events, Elastic Compute Service (ECS) events, Alibaba Cloud IoT events, and O&M events of some cloud services. This topic describes how to create and configure an event trigger for an Alibaba Cloud service in the Function Compute console. In this topic, an ECS trigger is used as an example.

Overview

After you submit a request to create a trigger in the Function Compute console, Function Compute creates an event rule in the default event bus in the following format: Service name-Function name-Trigger name. After the trigger is created, you can view information about the trigger in the Function Compute console. You can also view 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.

Precautions

  • You can create a maximum of 10 event rules for the system even bus named default in EventBridge. If the limit is exceeded, you cannot 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.
  2. In the left-side navigation pane, click Services and Functions.
  3. In the top navigation bar, select the region where the service resides.
  4. On the Services page, find the desired service and click Functions in the Actions column.
  5. On the Functions page, click the function that you want to manage.
  6. On the function details page, click the Triggers tab, select a version or alias from the Version or Alias drop-down list, and then click Create Trigger.
  7. 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 Asynchronous calls.
    Synchronous Invocation

    After the trigger is created, it is displayed on the Triggers tab. To modify or delete an existing 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.

  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, enter function code in the code editor, and then click Save.

    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);
      handle_event(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 Manage triggers.