Simple Message Queue (formerly MNS) queue triggers

Updated at:

Simple Message Queue (formerly MNS) queue triggers let you run a Function Compute function automatically whenever a message arrives in a queue. The trigger is backed by EventBridge: when you create the trigger in the Function Compute console, Function Compute creates the required event streams in EventBridge on your behalf. For details on event streams, see Overview.

Once the trigger is active, each message enqueued in the source queue causes the associated function to execute. Messages are delivered individually or in batches, depending on your batch configuration. This pattern suits end-to-end streaming data processing.

Constraints

  • The Simple Message Queue (formerly MNS) queue and the Function Compute function must be in the same region.

  • If your Alibaba Cloud account reaches the EventBridge event-stream limit, you cannot create additional Simple Message Queue (formerly MNS) queue triggers. For current limits, see Limits.

Prerequisites

Before you begin, ensure that you have:

Step 1: Create a trigger

  1. Log on to the Function Compute console and click the function you want to use.

  2. On the function details page, click the Configurations tab, then go to Triggers and click Create Trigger.

  3. Fill in the trigger settings. The key field is Invocation Method: For details, see Synchronous invocation and Asynchronous invocation.

    Invocation method Behavior Max payload When to use
    Sync Invocation Function Compute waits for the function to return before delivering the next event or batch 32 MB Order or sequential processing matters
    Async Invocation Function Compute delivers the event and immediately moves to the next one; the function runs in the background 128 KB High-throughput scenarios where you want to consume events quickly
  4. Click OK.

image

For advanced settings such as push settings, retry policies, and dead-letter queues, see Advanced features of triggers.

After the trigger is created, it appears on the Triggers tab. To modify or delete it, see Manage triggers.

Step 2: (Optional) Configure test parameters

The function receives Simple Message Queue (formerly MNS) events as an event parameter — an array of CloudEvents-formatted objects. To test your function code without sending a real message, simulate the event manually.

  1. On the Code tab, click the image.png icon next to Test Function and select Configure Test Parameters.

  2. In the Configure Test Parameters panel, click Create New Test Event or Modify Existing Test Event, enter an event name and the event content below, then click OK.

Sample event payload:

[
  {
    "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": "164901546557****",
    "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": "164901546557****",
    "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"
    }
  }
]

Event field reference

Each element in the array follows the CloudEvents specification.

CloudEvents envelope fields

Field Type Example Description
id String c2g71017-6f65-fhcf-a814-a396fc8d**** Unique event ID
source String MNS-Function-mnstrigger Event source identifier (the event bus name)
type String mns:Queue:SendMessage Event type
subject String acs:mns:cn-hangzhou:...:queues/zeus ARN of the source queue
time String 2021-04-08T06:28:17.093Z Time the message was enqueued (ISO 8601)
specversion String 1.0 CloudEvents specification version
datacontenttype String application/json; charset=utf-8 Content type of the data field
aliyunregionid String cn-chengdu Region where the queue resides
aliyunaccountid String 164901546557**** Alibaba Cloud account ID
aliyunpublishtime String 2021-10-15T07:06:34.028Z Time EventBridge published the event

`data` fields

Field Type Example Description
requestId String 606EA3074344430D4C81**** Unique ID of the send-message request
messageId String C6DB60D1574661357FA227277445**** Unique ID of the message
messageBody String TEST Message body content

Step 3: Write and test the function

After the trigger is created, write your function code, deploy it, then verify that the trigger works end-to-end.

Write and deploy function code

On the Code tab, write your handler and click Deploy Code.

The following Node.js example shows how to write function code:

'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 and process event.
  callback(null, 'return result');
}

Test the function

Use either method:

  • Simulated event: If you configured a test event in Step 2, click Test Function on the Code tab.

  • Real message: Log on to the Simple Message Queue (formerly MNS) console, select the source queue, and click Send Message.

image

After the function executes, view the output in Real-time Logs.

image

What's next