After Message Service (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
- Event mode
- Event stream mode
- Event mode: 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 mode: One or more messages are pushed to Function Compute for batch processing 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 mode.
- If the number of event streams exceeds the upper limit, the MNS queue triggers can no longer be created for the event stream mode.
For information about the limits on the number of resources involved in the trigger creation for a single Alibaba Cloud account in a single region, see Limits.
Before you begin
Step 1: Create a trigger
- Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
- In the top navigation bar, select a region. On the Services page, click the desired service.
- On the Functions page, click the function that you want to manage.
- 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.
- In the Create Trigger panel, specify related parameters. After you specify the parameters, click OK.The following table describes the basic parameters.
Parameter Description Example 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 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 Select a function invocation mode. Valid values:- Sync Invocation: This mode is suitable for sequential invocations. When a single event or a batch of events trigger the function, Function Compute runs the function and waits for a response before it processes the next event or batch of events. The upper limit of the payload for a synchronous invocation request is 32 MB. For more information, see Synchronous invocations.
- Async Invocation: This mode allows you to quickly consume events. When a single event or a batch of events trigger the function, Function Compute immediately returns a response and continue to process the next event or batch of events During this process, the function is run in asynchronous mode. The upper limit of the payload for an asynchronous invocation request is 128 KB. For more information, see Overview.
Sync Invocation Message Push Mode The underlying application mode that is used for pushing message data to Function Compute. Valid values:- Event Stream Mode: One or more messages are pushed to Function Compute for batch processing based on your batch configurations. This model is suitable for scenarios in which end-to-end streaming data is processed.
- Event Mode: 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 Mode Trigger State Specifies whether to enable the trigger immediately after it is created. By default, Enable Trigger is selected and the trigger is enabled after it is created. N/A For more information about advanced configurations such as message pushing, retry, and dead letter configurations, see Advanced features of triggers.
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
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.
- On the function details page, click the Code tab and click the
icon. From the drop-down list that appears, select Configure Test Parameters.
- 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
in the event mode:{ "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" } }
Sample code ofevent
in the event stream mode:[ { "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" } } ]
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.
- 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'); }
- 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.