All Products
Search
Document Center

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

Last Updated:Dec 09, 2025

Simple Message Queue (formerly MNS) topics can be used as event sources by using EventBridge. You can configure a topic trigger in Function Compute to trigger an associated function to perform custom processing on messages that are sent to the topic.

Background

Simple Message Queue (formerly MNS) is a distributed messaging service that features scalability, high efficiency, reliability, security, and availability. It allows application developers to transfer data and notifications between distributed components of an application and build a loosely coupled system. In Simple Message Queue (formerly MNS), messages are published to topics. A publisher can call the PublishMessage operation to publish a message to a topic. Subscribers to the topic receive the message. For more information, see PublishMessage.

Configuring a trigger for a topic is equivalent to registering a function as a subscriber to the topic. When a publisher publishes a message to the topic, the message is passed to the function and used as the event parameter of the function handler to trigger the function. For more information, see Basics.

Integration of Simple Message Queue (formerly MNS) and Function Compute provides the following benefits:

  • Allows you to perform advanced operations on a message before you send the message in an email or a text message.

  • Allows you not to create services at HTTP endpoints.

  • Supports abundant custom operations. For example, you can send a message to Slack or persist a specified message.

image

Prerequisites

Usage notes

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

  • You must avoid recursive loops.

    For example, you must avoid the following logic when you write code for a function: Topic A triggers Function B, and Function B publishes a new message to Topic A, which then triggers Function B again. This logic results in an infinite loop of function invocations.

Step 1: Create a topic and a trigger

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

  2. In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.

  3. On the details page of the function, click the Triggers tab. Then, click Create Trigger.

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

    Parameter

    Description

    Example

    Trigger Type

    Select Simple Message Queue (formerly MNS) Triggered by Topic Async Invocation.

    Simple Message Queue (formerly MNS) Triggered by Topic

    Name

    Customize the name of the trigger.

    trigger-mns

    Version or Alias

    Default value: LATEST. If you want to create a trigger for another version or alias, go back to the Function Details page and select a version or alias in the upper-right corner of the page. For more information about versions and aliases, see Manage versions and Manage aliases.

    LATEST

    MNS Region

    Select the region where the topic resides. The topic must reside in the same region as the function in Function Compute.

    China (Chengdu)

    Topic

    Select a topic from the list.

    Mytopic

    Filter Tag

    Specify a tag for message filtering.

    Function execution is triggered only when a message that contains the specified filtering tag is received.

    tag

    Event Format

    Select an event format. Valid values:

    • STREAM

    • JSON

    JSON

    Retry Policy

    Select a retry policy. Valid values:

    • Backoff Retry

    • Exponential Decay Retry

    For more information about how to specify the retry policy, see NotifyStrategy.

    Backoff Retry

    Role Name

    Select AliyunMNSNotificationRole.

    Note

    After you configure the preceding parameters, click OK. If you create a trigger of this type for the first time, click Authorize Now in the message that appears.

    AliyunMNSNotificationRole

Step 2: Configure the input parameters of the function

  1. On the Code tab of the function details page, click the image.png icon next Test Function and select Configure Test Parameters from the drop-down list.

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

    Messages that are published to a topic are processed based on the format of the message body. This constructs an event that is used to invoke a function in Function Compute. For more information, see NotifyContentFormat.

    • Examples on the format of the event content if you set the Event Format parameter to STREAM for a topic trigger:

      • The following sample code provides an example on the format of the event content if messages do not contain message attributes:

        Note

        If messages do not contain message attributes, the content of the event is a string in the JSON format.

        # The message body. 
        'hello topic'
      • The following sample code provides an example on the format of the event content if messages contain message attributes:

        Note

        The content of the event contains the key-value pairs that are related to message attributes. For more information, see PublishMessage.

            {
                "body": "hello topic",
                "attrs": {
                    "Extend": "{\\"key\\":\\"value\\"}"
                }
            }                    
    • Examples on the format of the event content if you set the Event Format parameter to JSON for a topic trigger:

      • The following sample code provides an example on the format of the event content if messages do not contain message attributes:

            {
                "TopicOwner": "118620210433****",
                "Message": "hello topic",
                "Subscriber": "118620210433****",
                "PublishTime": 1550216480040,
                "SubscriptionName": "test-fc-subscribe",
                "MessageMD5": "BA4BA9B48AC81F0F9C66F6C909C3****",
                "TopicName": "Mytopic",
                "MessageId": "2F5B3C082B923D4EAC694B76D928****"
            }        
                                    
      • The following sample code provides an example on the format of the event content if messages contain message attributes:

        Note

        The content of the event contains the key-value pairs that are related to message attributes. For more information, see PublishMessage.

            {
                "key": "value",
                "TopicOwner": "118620210433****",
                "Message": "hello topic",
                "Subscriber": "118620210433****",
                "PublishTime": 1550216302888,
                "SubscriptionName": "test-fc-subscribe",
                "MessageMD5": "BA4BA9B48AC81F0F9C66F6C909C3****",
                "TopicName": "Mytopic",
                "MessageId": "2F5B3C281B283D4EAC694B742528****"
            }
                                    

    The following table describes parameters of the event.

    Parameter

    Type

    Example

    Description

    key

    String

    value

    The key-value pairs that are related to message attributes.

    TopicOwner

    String

    118620210433****

    The account ID of the topic owner.

    Message

    String

    hello topic

    The content of the message.

    Subscriber

    String

    118620210433****

    The account ID of the user who subscribes to the topic.

    PublishTime

    Int

    1550216302888

    The time when the message was published.

    SubscriptionName

    String

    test-fc-subscribe

    The name of the subscription.

    MessageMD5

    String

    BA4BA9B48AC81F0F9C66F6C909C3****

    The MD5 hash value of the message body.

    TopicName

    String

    Mytopic

    The name of the topic.

    MessageId

    String

    2F5B3C281B283D4EAC694B742528****

    The message ID.

Step 3: Write the function code and test the function

After you create the topic trigger, you can write function code and test the function code to verify whether the code is valid.

  1. On the Code tab of the Function Details page, write code in the code editor and click Deploy.

    In this example, the function code is written in Python. The following sample code can be used as a function template for a topic trigger:

    import json
    import logging
    
    def handler(event, context):
      logger = logging.getLogger()
      logger.info("mns_topic trigger event = {}".format(event))
      # For example, you can record an event to Tablestore. 
      return "OK"                     
  2. Click Test Function.

    After the function is executed, you can view the result on the Code tab.

More information

In addition to the Function Compute console, you can configure triggers by using one of the following methods:

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