After MNS queues are integrated with Function Compute as event sources by using EventBridge, MNS queue triggers can trigger associated functions. The functions can be used to perform custom operations on the messages that are published to the MNS queues. This topic describes how to create an MNS queue trigger, configure function input parameters, and write and test code in the Function Compute console.

Features

After you submit a request to create a trigger in the Function Compute console, Function Compute creates EventBridge resources based on the trigger configurations. Function Compute provides the event model and the event stream model to push messages. The following items describe the resources that you can create in each model:
After a trigger is created, you can view the trigger information in the Function Compute console. You can also view the information about the created resources in the EventBridge console. When a message is sent to a MNS queue that is used as a trigger source, function execution is triggered in Function Compute. Different push models support different parameters. For more information, see Step 2: Configure the input parameters of the function.
  • Event model: A single message is pushed to a function as an event, the event follows the CloudEvents specification. For information about the relationship between the message content and CloudEvents, see Step 2: Configure the input parameters of the function.
  • Event stream model: One or more messages are pushed to a function as an event based on your batch configurations. This model is suitable for scenarios in which end-to-end streaming data is processed.

Usage notes

  • The MNS queue that is used as the trigger source must reside in the same region as the function in Function Compute.
  • If the number of custom event buses and event rules exceed the upper limits, the MNS queue triggers can no longer be created for the event model.
  • If the number of event streams exceeds the upper limit, the MNS queue triggers can no longer be created for the event stream model.
The following table describes the limits on the number of resources involved in the trigger creation for a single Alibaba Cloud account in a single region.
Item Upper limit
Number of custom event buses 10
Number of event rules created in each custom event bus 10
Number of event streams 30

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, click the desired service.
  3. On the Functions page, click the function that you want to manage.
  4. 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.
  5. In the Create Trigger panel, specify related parameters. After you specify the parameters, click OK.
    For information about the configurations of advanced features, see Advanced features of EventBridge triggers.
    Category Parameter Description Example
    Basic settings Trigger Type Select MNS Triggered by Queue. MNS
    Name Enter a custom trigger name mns-trigger
    Version or Alias The default value is LATEST. If you want to create a trigger for another version or alias, switch to the trigger of 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
    Queue Name Select a MNS queue. MyQueue
    Advanced settings Invocation Method Select a function invocation method.
    Valid values:
    • Synchronous Invocation: After an event triggers the execution of a function, Function Compute returns the result after 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 relatively higher scheduling latency. For more information, see Overview.
    Synchronous Invocation
    Message Push Model The underlying application model that is used for pushing message data to Function Compute.
    Valid values:
    • Event Model: A single message is passed to the function as an event parameter. The event follows the CloudEvents specification. For information about the relationship between the message content and CloudEvents, see Step 2: Configure the input parameters of the function.
    • Event Stream Model: One or more messages are pushed to Function Compute for batch processing based on your batch configuration. This model is suitable for scenarios in which end-to-end streaming data is processed.
    Event Model
    Batch Push

    This parameter is available only when you set Message Push Model to Event Stream Model.

    The batch push feature helps you aggregate multiple events at a time. This feature is triggered when one of the conditions that are specified in the Batch Push Messages and Batch Push Interval parameters is met.

    For example, you set Batch Push Messages to 100 and Batch Push Interval to 15 seconds. If the number of messages reaches 100 in 10 seconds, batch push is triggered immediately instead of 5 seconds later.

    Enable
    Batch Push Messages The maximum number of messages that are sent for each function invocation. Requests are sent only when the number of messages in the backlog reaches the specified value. Valid values: 1 to 500. 1
    Batch Push Interval The time interval at which the function is invoked. The system sends the aggregated messages to Function Compute at the specified time interval. Valid values: 0 to 15. Unit: seconds. A value of 0 indicates that messages are sent immediately after they are aggregated. 1
    Retry Policy

    This parameter is available only when you set Message Push Model to Event Stream Model.

    The retry policy that is used when a message fails to be pushed.

    Valid values:
    • Backoff Retry: A message can be retried up to three times. The interval between two consecutive retries is a random value between 10 and 20 seconds.
    • Exponential Decay Retry: The message can be retried 176 times. The interval between two consecutive retries increases exponentially to 512 seconds, and the total retry time is 1 day. The specific retry intervals are 1, 2, 4, 8, 16, 32, 64, 128, 256, and 512 seconds.
    Backoff Retry
    Fault Tolerance Policy

    This parameter is available only when you set Message Push Model to Event Stream Model.

    The method that is used to handle errors.

    Valid values:
    • Fault Tolerance Allowed: Fault tolerance is allowed. If an exception occurs, the event processing is not blocked. If the number of retries for a message exceeds the number of retries that is specified in the retry policy, the message is delivered to the dead-letter queue or discarded based on your configurations.
    • Fault Tolerance Prohibited: Fault tolerance is not allowed. If an exception occurs or the number of retries for a message exceeds the number of retries that is specified in the retry policy, the event processing is blocked.
    Fault Tolerance Allowed
    Dead-letter Queue

    This parameter is available only when you set Message Push Model to Event Stream Model.

    The queue to which the messages that are not processed or the messages whose number of retries exceeds the number of retries specified in the retry policy can be sent. If this feature is not enabled, the messages whose number of retries exceeds the number of retries that is specified in the retry policy are discarded.

    Enable
    Queue Type The type of the dead-letter queue.
    Valid values:
    • MNS
    • Message Queue for Apache RocketMQ
    MNS
    Queue Name The name of the dead-letter queue. test-queue

    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

An MNS event source is passed to a function in the form of event that acts as an input parameter. You can manually pass event to a function to simulate a trigger event and test whether the code of the function is correct.

  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 on the format of event in the event model:
    {
        "id":"c2g71017-6f65-fhcf-a814-a396fc8d****",
        "source":"MNS-Function-mnstrigger",
        "specversion":"1.0",
        "type":"mns:Queue:SendMessage",
        "datacontenttype":"application/json; charset=utf-8",
        "subject":"acs:mns:cn-hangzhou:164901546557****:queues/zeus",
        "time":"2021-04-08T06:28:17.093Z",
        "aliyunaccountid":"1649015465574023",
        "aliyunpublishtime":"2021-10-15T07:06:34.028Z",
        "aliyunoriginalaccountid":"164901546557****",
        "aliyuneventbusname":"MNS-Function-mnstrigger",
        "aliyunregionid":"cn-chengdu",
        "aliyunpublishaddr":"42.120.XX.XX",
        "data":{
            "requestId":"606EA3074344430D4C81****",
            "messageId":"C6DB60D1574661357FA227277445****",
            "messageBody":"TEST"
        }
    }
    Sample code on the format of event in the event stream model:
    [
        {
        "id":"c2g71017-6f65-fhcf-a814-a396fc8d****",
        "source":"MNS-Function-mnstrigger",
        "specversion":"1.0",
        "type":"mns:Queue:SendMessage",
        "datacontenttype":"application/json; charset=utf-8",
        "subject":"acs:mns:cn-hangzhou:164901546557****:queues/zeus",
        "time":"2021-04-08T06:28:17.093Z",
        "aliyunaccountid":"1649015465574023",
        "aliyunpublishtime":"2021-10-15T07:06:34.028Z",
        "aliyunoriginalaccountid":"164901546557****",
        "aliyuneventbusname":"MNS-Function-mnstrigger",
        "aliyunregionid":"cn-chengdu",
        "aliyunpublishaddr":"42.120.XX.XX",
        "data":{
            "requestId":"606EA3074344430D4C81****",
            "messageId":"C6DB60D1574661357FA227277445****",
            "messageBody":"TEST"
        }
        },
        {
        "id":"d2g71017-6f65-fhcf-a814-a396fc8d****",
        "source":"MNS-Function-mnstrigger",
        "specversion":"1.0",
        "type":"mns:Queue:SendMessage",
        "datacontenttype":"application/json; charset=utf-8",
        "subject":"acs:mns:cn-hangzhou:164901546557****:queues/zeus",
        "time":"2021-04-08T06:28:17.093Z",
        "aliyunaccountid":"1649015465574023",
        "aliyunpublishtime":"2021-10-15T07:06:34.028Z",
        "aliyunoriginalaccountid":"164901546557****",
        "aliyuneventbusname":"MNS-Function-mnstrigger",
        "aliyunregionid":"cn-chengdu",
        "aliyunpublishaddr":"42.120.XX.XX",
        "data":{
            "requestId":"606EA3074344430D4C81****",
            "messageId":"C6DB60D1574661357FA227277445****",
            "messageBody":"TEST"
        }
        }
    ]
    The following table describes the parameters in data. For information about the parameters that are defined in the CloudEvents specification, see Overview.
    Parameter Type Example Description
    requestId String 606EA3074344430D4C81**** The ID of the request. The ID of each request is unique.
    messageId String C6DB60D1574661357FA227277445**** The ID of the message. The ID of each message is unique.
    messageBody String TEST The content of the message.

Step 3: Write the function code and test the function

After you create the 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 custom MNS event sources are delivered to Function Compute by using EventBridge, the trigger automatically triggers the function execution.

  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 Manage triggers.