All Products
Search
Document Center

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

Last Updated:Sep 19, 2024

Simple Message Queue (formerly MNS) topics can be used as event sources to integrate Simple Message Queue (formerly MNS) with Function Compute by using EventBridge. You can configure an Simple Message Queue (formerly MNS) topic trigger in Function Compute. The associated function is automatically triggered when new messages are sent to the topic to perform custom operations on the messages.

Overview

Simple Message Queue (formerly MNS) is a distributed messaging service that features high scalability, efficiency, reliability, security, and convenience. MNS allows application developers to transfer data and notifications between the 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, and a subscriber to the topic receives the message. For more information, see PublishMessage.

Configuring a trigger for an Simple Message Queue (formerly MNS) topic is equivalent to registering a function as a subscriber to the Simple Message Queue (formerly MNS) topic. When a publisher publishes a message to the Simple Message Queue (formerly MNS) 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.

The following benefits are delivered after Simple Message Queue (formerly MNS) and Function Compute are integrated:

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

  • Does not require you 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

Before you start

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

    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 an Simple Message Queue (formerly 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 desired service and click Functions in the Actions column.

  3. On the Functions page, click the function that 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, specify the 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

    The default value is 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

    MNS Region

    Select the region where the topic resides. The Simple Message Queue (formerly MNS) topic must reside in the same region as the Function Compute function.

    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 select a retry policy, see NotifyStrategy.

    Backoff Retry

    Role Name

    Select AliyunMNSNotificationRole.

    Note

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

    AliyunMNSNotificationRole

    After the trigger is created, it is displayed on the Triggers tab. To modify or delete an existing trigger, see Manage triggers.

Step 2: Configure the input parameters of the function

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

  2. In the Configure Test Parameters panel, select the Create New Test Event or Modify Existing Test Event tab, configure Event Name and the event content in the code editor, and then click OK.

    Messages that are published on an Simple Message Queue (formerly MNS) topic are processed based on notifyContentFormat, which is the event of the handler function. For more information, see NotifyContentFormat.

    • If you set Event Format to STREAM:

      • The following code shows the format of the event parameter if messages do not contain message attributes:

        Note

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

        # The message body. 
        'hello topic'
      • The following code shows the format of the event parameter if messages contain message attributes:

        Note

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

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

      • The following code shows the format of the event parameter 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 code shows the format of the event parameter if messages contain message attributes:

        Note

        The content of the event parameter 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 the fields in the event parameter.

    Field

    Value type

    Example

    Description

    key

    String

    value

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

    TopicOwner

    String

    118620210433****

    The ID of the account that subscribed to the topic.

    Message

    String

    hello topic

    The content of the message.

    Subscriber

    String

    118620210433****

    The account ID of the user.

    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 MNS topic.

    MessageId

    String

    2F5B3C281B283D4EAC694B742528****

    The message ID.

Step 3: Write function code and test the function

After you create the Simple Message Queue (formerly MNS) topic trigger, you can write function code and test the function to verify whether the code is correct.

  1. 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 Python. The following sample code can be used as a function template for an 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 an event to Tablestore. 
      return "OK"                     
  2. On the Code tab, click Test Function.

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

Additional information

In addition to the Function Compute console, you can configure triggers by using the following methods:
  • Use Serverless Devs to configure triggers. For more information, see Serverless Devs.
  • Use SDKs to configure triggers. For more information, see SDKs.

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