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

Overview

After you submit a request to create a trigger in the Function Compute console, Function Compute creates EventBridge resources that are named in the following formats based on the trigger configurations:
  • Event bus: MNS-<Function name>-<Trigger name>.
  • Event source: MNS-<Function name>-<Trigger name>.
  • Event rule: <Service name>-<Function name>-<Trigger name>.
  • Event target ID: <Function name>-<Trigger name>.
After the 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 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.
Note The names of the created EventBridge resources must meet the naming rules. Otherwise, you cannot create the trigger. For more information, see Limits.

Usage notes

  • In EventBridge, the number of custom event buses in the same region cannot exceed 10, and the number of event rules that are created on each custom bus cannot exceed 10. If the limits are reached, you cannot create EventBridge triggers.
  • The created MNS queue and the Function Compute functions must be in the same region.
  • You cannot create MNS queue triggers by using Serverless Devs or SDKs.

Prerequisites

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.
    hanshuguanli
  6. 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.
  7. In the Create Trigger panel, specify related parameters. After you specify the parameters, click OK.
    Create Trigger panel
    Parameter Description Example
    Trigger Type Select MNS from the drop-down list. MNS
    Name Enter a custom trigger name mns-trigger
    Version or Alias The default value is LATEST. If you want to create a trigger of 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
    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 relatively higher scheduling latency. For more information, see Asynchronous calls.
    Synchronous Invocation

    After the trigger is created, it is displayed on the Triggers tab.

Step 2: Write a function

After you create an MNS queue trigger, you can write the function code.

On the function details page, click the Code tab, enter function code in the code editor, and then click Save.

The following sample code provides 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');
}

Step 3: Test the function

After you write the function, you must debug the function to verify whether the code is correct. When the events that are generated by the MNS custom event sources are delivered to Function Compute by using EventBridge, function execution is automatically triggered.

An MNS event is passed to the function as an input parameter. You can manually pass an event to the function to trigger the function 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.

    The event parameter is an input parameter of Function Compute. The following code shows the format of the event parameter:

    {
        "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"
        }
    }
    The following table describes the parameters in the data field. For more information about the parameter definition 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.
  3. On the function details page, click the Code tab and click Test Function.

Verify results

After the function is executed, a message appears on the Code tab to indicate that the execution is successful.

result

References

To modify or delete an existing trigger, see Manage triggers.