When a publisher sends a message to an MNS topic, Function Compute can automatically invoke a function with that message as the event payload—no HTTP endpoint required. Use this integration to pre-process messages before delivery, fan out to external services, or persist specific messages to a data store.
How it works
Configuring an MNS topic trigger registers your function as a subscriber to the topic. When a publisher calls the PublishMessage operation on the topic, the message passes directly to your function's handler as the event parameter. For background on the trigger model, see Basics.
Prerequisites
Before you begin, ensure that you have:
A Function Compute service — Create a service
A Function Compute function — Create a function
An MNS topic in the same region as your function — Create a topic
Usage notes
Avoid recursive invocations. If your function publishes a new message back to the same topic that triggered it, the function will be invoked again indefinitely. Design your function logic to prevent this loop.
The MNS topic and the Function Compute function must reside in the same region.
Step 1: Create an MNS topic 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, find the target service and click Functions in the Actions column.
On the Functions page, click the function you want to modify.
On the function details page, click the Triggers tab, select a version or alias from the Version or Alias drop-down list, and then click Create Trigger.
In the Create Trigger panel, configure the following parameters and click OK.
| Parameter | Description | Example |
|---|---|---|
| Trigger type | Select Simple Message Queue (formerly MNS) Triggered by Topic. | Simple Message Queue (formerly MNS) Triggered by Topic |
| Name | Enter a trigger name. | trigger-mns |
| Version or alias | Defaults to LATEST. To target another version or alias, select it from the upper-right corner of the function details page. For details, see Manage versions and Manage aliases. | LATEST |
| MNS region | Select the region where the topic resides. The topic must be in the same region as the function. | China (Chengdu) |
| Topic | Select a topic from the list. | Mytopic |
| Filter tag | Specify a tag for message filtering. The function is invoked only when an incoming message contains this tag. | tag |
| Event format | Select STREAM or JSON. See Event format reference below for the differences. | JSON |
| Retry policy | Select Backoff Retry or Exponential Decay Retry. For guidance, see NotifyStrategy. | Backoff Retry |
| Role name | Select AliyunMNSNotificationRole. If this is your first trigger of this type, click Authorize Now in the dialog box that appears. | AliyunMNSNotificationRole |
After the trigger is created, it appears on the Triggers tab. To modify or delete a trigger, see Manage triggers.
Step 2: Understand the event format
The event your function receives depends on the Event format you selected when creating the trigger and whether the message includes attributes. For more information, see NotifyContentFormat.
Event format reference
STREAM format
Without message attributes, the event is a plain string:
'hello topic'With message attributes, the event is a JSON object:
{
"body": "hello topic",
"attrs": {
"Extend": "{\"key\":\"value\"}"
}
}JSON format
Without message attributes:
{
"TopicOwner": "118620210433****",
"Message": "hello topic",
"Subscriber": "118620210433****",
"PublishTime": 1550216480040,
"SubscriptionName": "test-fc-subscribe",
"MessageMD5": "BA4BA9B48AC81F0F9C66F6C909C3****",
"TopicName": "Mytopic",
"MessageId": "2F5B3C082B923D4EAC694B76D928****"
}With message attributes, additional key-value pairs from PublishMessage appear at the top level:
{
"key": "value",
"TopicOwner": "118620210433****",
"Message": "hello topic",
"Subscriber": "118620210433****",
"PublishTime": 1550216302888,
"SubscriptionName": "test-fc-subscribe",
"MessageMD5": "BA4BA9B48AC81F0F9C66F6C909C3****",
"TopicName": "Mytopic",
"MessageId": "2F5B3C281B283D4EAC694B742528****"
}JSON event field reference
| Field | Type | Example | Description |
|---|---|---|---|
| key | String | value | Key-value pairs from message attributes |
| TopicOwner | String | 118620210433**** | The ID of the account that subscribed to the topic |
| Message | String | hello topic | Message content |
| Subscriber | String | 118620210433**** | Account ID of the subscriber |
| PublishTime | Int | 1550216302888 | The time when the message was published |
| SubscriptionName | String | test-fc-subscribe | Name of the subscription |
| MessageMD5 | String | BA4BA9B48AC81F0F9C66F6C909C3**** | MD5 hash of the message body |
| TopicName | String | Mytopic | Name of the MNS topic |
| MessageId | String | 2F5B3C281B283D4EAC694B742528**** | Unique message ID |
Configure a test event
On the function details page, click the Code tab, then click the
icon next to Test Function and select Configure Test Parameters.In the Configure Test Parameters panel, select Create New Test Event or Modify Existing Test Event, enter an Event Name, paste one of the event examples above into the editor, and click OK.
Step 3: Write and test function code
The following Python example shows how to handle an MNS topic trigger event. You can use it as a function template and add your custom processing logic.
On the function details page, click the Code tab, enter your function code in the editor, and click Deploy.
import json
import logging
def handler(event, context):
logger = logging.getLogger()
logger.info("mns_topic trigger event = {}".format(event))
# For example, record an event to Tablestore.
return "OK"Click Test Function on the Code tab.
After the function runs, the execution result and logs appear on the Code tab.
Next steps
Configure triggers using alternative tools:
Serverless Devs — see Trigger-related commands
SDKs — see SDKs
To modify or delete an existing trigger, see Manage triggers.