All Products
Search
Document Center

Function Compute:Message Queue for MNS topic triggers

Last Updated:Apr 16, 2026

Simple Message Queue (formerly MNS) topics can act as event sources and integrate with EventBridge and Function Compute. A Simple Message Queue (formerly MNS) topic trigger automatically runs the associated function when a new message is sent to your Simple Message Queue (formerly MNS) topic. This allows you to easily implement custom processing for incoming messages.

Background information

Message Queue for MNS is an efficient, reliable, secure, easy-to-use, and elastically scalable distributed messaging service. It helps application developers pass data and notifications across distributed components and build loosely coupled systems. In Message Queue for MNS, a topic is the destination where messages are published. A publisher can call the PublishMessage API to publish a message to a topic, and the topic subscribers receive the message. For API details, see PublishMessage.

Configuring a Simple Message Queue (formerly MNS) topic trigger registers your function as a subscriber to the Simple Message Queue (formerly MNS) topic. When a publisher sends a message to the Simple Message Queue (formerly MNS) topic, the message content is delivered to the function to trigger its execution. The message content is passed as the event parameter to the function handler. For more information, see Basic information.

The integration of Message Queue for MNS and Function Compute provides the following benefits:

  • Perform advanced processing on messages before you send them by email or SMS.

  • You do not need to host a service for an HTTP endpoint.

  • Implement rich custom processing, such as sending messages to Slack or persisting specific messages.

Prerequisites

Usage notes

  • The topic in Message Queue for MNS and the function in Function Compute must be in the same region.

  • Avoid recursive invocations.

    When you write function code, make sure that the function does not contain the following logic: Topic A triggers Function B, and Function B publishes a new message to Topic A. This may cause infinite recursive invocations.

Step 1: Create a Message Queue for MNS topic 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 function details page, select the Trigger tab, and then click Create Trigger.

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

    Parameter

    Actions

    Example

    Trigger Type

    Select Message Queue for MNS Topic Trigger - Asynchronous Invocation.

    Simple Message Queue (formerly MNS) topic trigger

    Name

    Enter a custom trigger name.

    trigger-mns

    Version or Alias

    The default value is LATEST. If you want to create a trigger for another version or alias, first switch to that version or alias in the upper-right corner of the function details page. For an overview of versions and aliases, see Manage versions and Manage aliases.

    LATEST

    MNS region

    Select the region where the topic is located. The Simple Message Queue (formerly MNS) topic and the Function Compute function must be in the same region.

    China (Chengdu)

    Topic

    Select the topic from the list.

    Mytopic

    Filter tag

    Enter a message filter tag.

    The function is triggered only when it receives messages that contain the specified filter tag.

    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 guidance on how to choose a retry policy, see NotifyStrategy.

    Backoff Retry

    Role Name

    Select AliyunMNSNotificationRole.

    Note

    If this is your first time creating this type of trigger, after you click OK, select Authorize Now in the dialog box that appears.

    AliyunMNSNotificationRole

Step 2: Configure function input parameters

  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, select Create New Test Event or Modify Existing Test Event. Enter an event name and event content, and then click OK.

    Messages published to a Simple Message Queue (formerly MNS) topic are processed based on the notifyContentFormat. The processed message is the event parameter for the function handler. For more information, see NotifyContentFormat.

    • If you set Event Format to STREAM when you create the trigger:

      • If the message does not include message attributes (MessageAttributes), the event format is as follows.

        Note

        If the message does not include message attributes (MessageAttributes), the event content is the raw message body as a string.

        # Message body.
        'hello topic'
      • If the message includes message attributes (MessageAttributes), the event format is as follows.

        Note

        The event content includes key-value pairs related to MessageAttributes. For more information, see PublishMessage.

            {
                "body": "hello topic",
                "attrs": {
                    "Extend": "{\\"key\\":\\"value\\"}"
                }
            }                    
    • If you set Event Format to JSON when you create the trigger:

      • If the message does not include message attributes (MessageAttributes), the event format is as follows.

            {
                "TopicOwner": "118620210433****",
                "Message": "hello topic",
                "Subscriber": "118620210433****",
                "PublishTime": 1550216480040,
                "SubscriptionName": "test-fc-subscribe",
                "MessageMD5": "BA4BA9B48AC81F0F9C66F6C909C3****",
                "TopicName": "Mytopic",
                "MessageId": "2F5B3C082B923D4EAC694B76D928****"
            }        
                                    
      • If the message includes message attributes (MessageAttributes), the event format is as follows.

        Note

        The event content includes key-value pairs related to MessageAttributes. 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 the fields in the event parameter.

    Parameter

    Type

    Example

    Description

    key

    String

    value

    Key-value pairs from message attributes.

    TopicOwner

    String

    118620210433****

    The ID of the account that owns the topic.

    Message

    String

    hello topic

    The message content.

    Subscriber

    String

    118620210433****

    The user's account ID.

    PublishTime

    Int

    1550216302888

    The time when the message was published.

    SubscriptionName

    String

    test-fc-subscribe

    The subscription name.

    MessageMD5

    String

    BA4BA9B48AC81F0F9C66F6C909C3****

    The MD5 hash of the message body.

    TopicName

    String

    Mytopic

    The topic name.

    MessageId

    String

    2F5B3C281B283D4EAC694B742528****

    The message ID.

Step 3: Write and test the function code

After you create a Simple Message Queue (formerly MNS) topic trigger, write and test your function code to verify that it works as expected.

  1. On the Code tab of the function details page, write code in the editor, and then click Deploy.

    This section uses Python code as an example. You can use the following sample code as a template for a Simple Message Queue (formerly MNS) topic trigger.

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

    After the execution is complete, you view the execution result above the Code tab.

More information

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

  • Use Serverless Devs tool to configure triggers. For more operations, please refer to Common Serverless Devs commands.

  • Use SDKs to configure triggers. For more information, see SDKs.

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