All Products
Search
Document Center

Function Compute:Simple Message Queue (formerly MNS) topic triggers

Last Updated:Apr 01, 2026

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.

image

Prerequisites

Before you begin, ensure that you have:

Usage notes

Warning

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

  1. Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.

  2. In the top navigation bar, select a region. On the Services page, find the target service and click Functions in the Actions column.

  3. On the Functions page, click the function you want to modify.

  4. 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.

  5. In the Create Trigger panel, configure the following parameters and click OK.

ParameterDescriptionExample
Trigger typeSelect Simple Message Queue (formerly MNS) Triggered by Topic.Simple Message Queue (formerly MNS) Triggered by Topic
NameEnter a trigger name.trigger-mns
Version or aliasDefaults 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 regionSelect the region where the topic resides. The topic must be in the same region as the function.China (Chengdu)
TopicSelect a topic from the list.Mytopic
Filter tagSpecify a tag for message filtering. The function is invoked only when an incoming message contains this tag.tag
Event formatSelect STREAM or JSON. See Event format reference below for the differences.JSON
Retry policySelect Backoff Retry or Exponential Decay Retry. For guidance, see NotifyStrategy.Backoff Retry
Role nameSelect 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

FieldTypeExampleDescription
keyStringvalueKey-value pairs from message attributes
TopicOwnerString118620210433****The ID of the account that subscribed to the topic
MessageStringhello topicMessage content
SubscriberString118620210433****Account ID of the subscriber
PublishTimeInt1550216302888The time when the message was published
SubscriptionNameStringtest-fc-subscribeName of the subscription
MessageMD5StringBA4BA9B48AC81F0F9C66F6C909C3****MD5 hash of the message body
TopicNameStringMytopicName of the MNS topic
MessageIdString2F5B3C281B283D4EAC694B742528****Unique message ID

Configure a test event

  1. On the function details page, click the Code tab, then click the drop-down icon icon next to Test Function and select Configure Test Parameters.

  2. 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.

  1. 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"
  1. 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:

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