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 to trigger an associated function to perform custom processing on messages that are sent to the topic.

Overview

Simple Message Queue (formerly MNS) is a distributed messaging service that features high scalability, efficiency, reliability, security, and availability. Simple Message Queue (formerly MNS) 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 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.

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

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 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 an Simple Message Queue (formerly 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, click the Configurations tab. In the left-side navigation pane, click Triggers. Then, click Create Trigger.

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

    Parameter

    Description

    Example

    Trigger Type

    The type of the trigger. Select Simple Message Queue (formerly MNS) Triggered by Topic.

    Simple Message Queue (formerly MNS) Triggered by Topic

    Name

    The name of the trigger.

    trigger-mns

    Version or Alias

    The version or alias of the trigger. Default value: 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

    Simple Message Queue (formerly MNS) Region

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

    China (Chengdu)

    Topic

    The name of the existing Simple Message Queue (formerly MNS) topic.

    Mytopic

    Filter Tag

    The tag that is used for message filtering.

    The function is invoked only when a message that contains the specified filtering tag is received.

    tag

    Event Format

    The format of the event. Options:

    • STREAM

    • JSON

    JSON

    Retry Policy

    The retry policy. Options:

    • 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 an Simple Message Queue (formerly MNS) 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 an Simple Message Queue (formerly MNS) 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 an Simple Message Queue (formerly MNS) 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 Simple Message Queue (formerly MNS) topic owner.

    Message

    String

    hello topic

    The content of the message.

    Subscriber

    String

    118620210433****

    The account ID of the user who subscribes to the Simple Message Queue (formerly MNS) 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 Simple Message Queue (formerly 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 code to verify whether the code is valid.

  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, you can record an event to Tablestore. 
      return "OK"                     
  2. Click Test Function.

    After the function is executed, you can view the results 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.