After ApsaraMQ for MQTT is integrated with Function Compute by using EventBridge, ApsaraMQ for MQTT triggers can be used to invoke functions. This way, you can invoke functions to process the messages that are published to ApsaraMQ for MQTT based on your business requirements. This topic describes how to create an ApsaraMQ for MQTT trigger, configure the input parameters of a function, and write and test function code in the Function Compute console.
Background information
After you submit a request to create a trigger in the Function Compute console, Function Compute automatically creates event streams in EventBridge based on the configurations of the trigger.
After the trigger is created, you can view the information about the trigger in the Function Compute console. You can also view the information about the created resources in the EventBridge console. When a message is queued in ApsaraMQ for MQTT, the associated function in Function Compute is invoked. One or more message events are pushed to the function in batches for processing based on your batch configurations.
Prerequisites
EventBridge
EventBridge is activated, and the required permissions are granted to a Resource Access Management (RAM) user. For more information, see Activate EventBridge and grant permissions to a RAM user.
Function Compute
A function is created. For more information, see the Create a function section of the "Manage functions" topic.
ApsaraMQ for MQTT
An ApsaraMQ for MQTT instance is created. For more information, see the Step 1: Create resources section of the "Quick Start" topic.
A topic is created. For more information, see the Create a topic section of the "Manage topics" topic.
Usage notes
The ApsaraMQ for MQTT instance that serves as the trigger source must reside in the same region as the function to be invoked in Function Compute.
If the number of created event streams reaches the upper limit, no more ApsaraMQ for MQTT triggers can be created. For more information about the limits on the number of event streams, see Limits.
Step 1: Create an ApsaraMQ for MQTT trigger
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
On the function details page, click the Configurations tab. In the left-side navigation pane, click Triggers. Then, click Create Trigger.
In the Create Trigger panel, configure the parameters and click OK.
Parameter
Description
Example
Trigger Type
The type of the trigger. Select ApsaraMQ for MQTT.
ApsaraMQ for MQTT
Name
The name of the trigger.
mqtt-trigger
Version or Alias
The version or alias of the trigger. Default value: LATEST. If you want to create a trigger for another version or alias, select a version or alias in the upper-right corner of the function details page. For more information about versions and aliases, see Manage versions and Manage aliases.
LATEST
ApsaraMQ for MQTT Instance
The name of the existing ApsaraMQ for MQTT instance.
mqtt-xxx
MQTT Topic
The name of the existing ApsaraMQ for MQTT topic.
testTopic
Invocation Method
The mode in which the function is invoked.
Valid values:
Sync Invocation (default): After an event invokes the function, Function Compute waits for the function invocation to complete before it returns a response. For more information, see Synchronous invocations.
Async Invocation: After an event invokes the function, Function Compute immediately returns a response and ensures that the function is successfully executed at least once. However, the detailed results are not returned. This mode is suitable for functions that have higher scheduling latency. For more information, see Overview.
Sync Invocation
Trigger State
Specifies whether to enable the trigger immediately after it is created. By default, Enable Trigger is selected. The trigger is immediately enabled after it is created.
Enable Trigger
For more information about advanced configurations such as push settings, retry policies, and dead-letter queues, see Advanced features of triggers.
The trigger is displayed on the Triggers tab after it is created. For more information about how to modify or delete a created trigger, see Manage triggers.
Step 2: Configure the input parameters of the function
An ApsaraMQ for MQTT event is used to invoke a function in Function Compute. The parameters of the event are used as the input parameters of the function. You can manually pass the parameters of the event to invoke the function as a test.
On the Code tab of the function details page, click the icon next Test Function and select Configure Test Parameters from the drop-down list.
In the Configure Test Parameters panel, click the Create New Test Event or Modify Existing Test Event tab, enter the event name and event content, and then click OK.
The following sample code provides an example on the format of the event content:
[ { "props": { "firstTopic": "testTopic", "secondTopic": "/testMq4****", "clientId": "consumerGroupID@@@xxx" }, "body": "hello mq4Iot pub sub msg" } ]
The following table describes the parameters of the event.
Parameter
Type
Example
Description
props
Map
The attributes of the message.
firstTopic
String
testTopic
The parent topic that is used to send and receive messages.
secondTopic
String
/testMq4****
The child topic.
clientId
String
consumerGroupID@@@xx
The client ID.
body
String
hello mq4Iot pub sub msg
The content of the message.
Step 3: Write and test function code
After you create the trigger, you can write function code and test the function code to verify whether the code is valid. When ApsaraMQ for MQTT receives a message, the trigger automatically invokes the function.
On the function details page, click the Code tab, enter function code in the code editor, and then click Deploy.
In this example, the function code is written in Node.js. 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); // Parse the event parameters and process the event. callback(null, 'return result'); }
Click Test Function.
More information
In addition to the Function Compute console, you can configure triggers by using one of the following methods:
Use SDKs to configure triggers. For more information, see SDKs.
To modify or delete an existing trigger, see Manage triggers.