This topic describes how to configure MNS topic triggers (hereinafter referred to as MNS topic triggers) in the Function Compute console, including creating triggers, writing functions, and testing functions.

Prerequisites

Procedure

  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 service that you want to manage and click Functions in the Actions column.
  5. On the Functions page, click the name of the function that you want to manage.
    hanshuguanli
  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.
    Create a webhook
  7. In the Create Trigger panel, enter the related information. Then, click OK.
    Create Trigger Panel-mntopic
    Parameter Configuration method Example
    Trigger Type Select MNS. MNS
    Parameter Enter a custom trigger name. trigger-mns
    Version or alias The default value is LATEST. If you want to create a trigger of another version or alias, you must switch to this version or alias in the upper-right corner of the function details page. For more information about the version, see Introduction to versions. LATEST
    MNS region Select the region where the topic resides.

    We strongly recommend that MNS topics and Function Compute functions be deployed in the same region. Otherwise, the network latency will increase.

    China (Chengdu)
    Subject Select the created topic from the list. Mytopic
    Filter messages Enter the message filter label.

    Function execution is triggered only when a message is received that contains the filter label string set here.

    tag
    The format of the event. Select Event Format. Valid values:
    • STREAM
    • JSON
    JSON
    Retry policies Select retry policy. Valid values:
    • backoff retry
    • exponential decay

    For more information about how to select a retry policy, see NotifyStrategy.

    Backoff Retry
    Role Name Select AliyunMNSNotificationRole.
    Note If you create a trigger of this type for the first time, click OK and select Authorize Now in the dialog box that appears.
    AliyunMNSNotificationRole

    After the trigger is created, the created trigger is displayed in the Trigger Name list.

    trigger-mns

Write code for a function

After you create an MNS topic trigger, you can start writing function code.

On the function details page, click the Function Code tab and write code in the code editor.
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, log an event to a Tablestore. 
  return "OK"                     

Test a function

After you define a function, you must debug it to verify the correctness of its code.

  1. On the Function Details page, click the Function Code tab, and then click the xialatubiao icon. From the drop-down list, select Configure Test Parameters.
  2. In the Configure Test Parameters dialog box, click the Create New Test Event or Edit Existing Test Event tab, and specify the Event Name and event content. Click OK.

    Messages published on an MNS topic are processed according to the notifyContentFormat, that is, the event of the entry function. For more information, see NotifyContentFormat.

    • When you create a trigger, if the event format is set to STREAM.
      • If the message attribute (MessageAttributes) is excluded from the message, the event format is as follows.

        Note If the message attribute (MessageAttributes) is excluded from the message, the content of the event is formatted as a JSON string.
        # The message body. 
        'hello topic'
      • If the message contains a message attribute (MessageAttributes), the event format is as follows.

        Note The content of the event contains MessageAttributes-related key-value pairs. For more information, see PublishMessage.
            {
                "body": "hello topic",
                "attrs": {
                    "Extend": "{\\"key\\":\\"value\\"}"
                }
            }                    
    • When you create a trigger, if the event format is set to JSON.
      • If the message attribute (MessageAttributes) is excluded from the message, 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 contains a message attribute (MessageAttributes), the event format is as follows.

        Note The content of the event contains MessageAttributes-related key-value pairs. 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 explains the parameters involved in the event.
    Filed Type Example Description
    key String value Key-value pairs related to message attributes.
    TopicOwner String 118620210433**** The AccountId of the subscribed topic.
    Message String hello topic The content of the message.
    Subscriber String 118620210433**** The AccountId of the user.
    PublishTime Map 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.
  3. On the function details page, click the Function Code tab, and then click Test Function.

Additional information

In addition to the Function Compute console, you can configure triggers in the following ways:
  • Use the Serverless Devs tool to configure triggers. For more information, see Serverless Devs.
  • Configure triggers by using the SDK. For more information, see Supported SDKs.

To modify or delete a created trigger, see Manage triggers.