This topic describes how to configure an MNS topic trigger for a function in the Function Compute console, including how to create an MNS topic trigger, configure input parameters, write function code, and test the function.

Before you start

Step 1: Create an MNS topic trigger

  1. Log on to the Function Compute console.
  2. In the left-side navigation pane, click Services and Functions.
  3. In the top navigation bar, select the region where your Kubernetes cluster is deployed.
  4. On the Services page, find the desired service and click Functions in the Actions column.
  5. On the Functions page, click the function that you want to manage.
  6. On the function details page, click the Triggers tab, select the version or alias from the Version or Alias drop-down list, and then click Create Trigger.
  7. In the Create Trigger panel, enter the related information. Then, click OK.
    Parameter Description Example
    Trigger Type Select MNS (Triggered by Topic). MNS
    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 of a service, see Manage versions and Manage aliases. LATEST
    MNS Region Select the region where the topic resides.
    Note We recommend that you deploy the MNS topic in the same region as the region of Function Compute functions. Otherwise, the network latency increases.
    China (Chengdu)
    Subject 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 Select AliyunMNSNotificationRole.
    Note After you configure the preceding parameters, click OK. If this is the first time that 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 parameter of the function

  1. On the function details page, click the Code tab and click the xialatubiao icon. From the drop-down list that appears, select Configure Test Parameters.
  2. In the Configure Test Parameters panel, click the Create New Test Event or Modify Existing Test Event tab, and specify Event Name and the event content. After you specify the parameters, click OK.

    Messages that are published on an MNS topic are processed in the form of notifyContentFormat, which is the event of the handler function. For more information, see NotifyContentFormat.

    • If you select STREAM for Event Format:
      • 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 select JSON for Event Format:
      • 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 parameters in event.
    Parameter 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 of the message body.
    TopicName String Mytopic The name of the topic.
    MessageId String 2F5B3C281B283D4EAC694B742528**** The ID of the message.

Step 3: Write the function code and test the function

After you create the 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 Save.
    In this example, the function code is written in Python. The following sample code can be used as a function template for an 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. Click the Code tab and click Test Function.
    After the function is executed, you can view the result on the Code tab.

References

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 Supported SDKs.

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