After creating an MNS Topic Trigger, you can write the code of a function. This topic describes how to write a function in the Function Compute console.

Prerequisites

Create a trigger

Write function code in Python

  1. Log on to the Function Compute console.
  2. In the top navigation bar, select a region.
  3. In the left-side navigation pane, click Service/Function.
  4. Locate the target function in the target service and click the function name.
  5. Click the Code tab and write code in the code editor.
    This topic uses the code for a Python function as an example. The following sample code can be used as a template for MNS topic triggers.
    import json
    import logging
    
    def handler(event, context):
      logger = logging.getLogger()
      logger.info("mns_topic trigger event = {}".format(event))
      # do your things, for example, record event to tablestore
      return "OK"                     

Event format

Messages published to MNS Topics are processed based on the notifyContentFormat, which is the Handler Function event. For more information, see NotifyContentFormat.

  • When you create a trigger, if Event Format is set to STREAM.
    • If the message does not contain message attibutes (MessageAttributes), the event format is as follows:

      Note If the message does not contain message attibutes (MessageAttributes), the content of the event is formatted as a JSON string.
      # message body
      'hello topic'
    • If the message does not contain message attibutes (MessageAttributes), the event format is as follows:.

      Note The content of an event contains key-value pairs related to MessageAttributes. For more information, see PublishMessage.
          {
              "body": "hello topic",
              "attrs": {
                  "Extend": "{\\"key\\":\\"value\\"}"
              }
          }                    
  • When you create a trigger, if Event Format is set to JSON.
    • If the message does not contain message attibutes (MessageAttributes), the event format is as follows:

          {
              "TopicOwner": "1186202104331798",
              "Message": "hello topic",
              "Subscriber": "1186202104331798",
              "PublishTime": 1550216480040,
              "SubscriptionName": "test-fc-subscibe",
              "MessageMD5": "normal",
              "TopicName": "test-topic",
              "MessageId": "2F5B3C082B923D4EAC694B76D928B5B8"
          }        
      							
    • If the message does not contain message attibutes (MessageAttributes), the event format is as follows:.

      Note The content of an event contains key-value pairs related to MessageAttributes. For more information, see PublishMessage.
          {
              "key": "value",
              "TopicOwner": "1186202104331798",
              "Message": "hello topic",
              "Subscriber": "1186202104331798",
              "PublishTime": 1550216302888,
              "SubscriptionName": "test-fc-subscibe",
              "MessageMD5": "normal",
              "TopicName": "test-topic",
              "MessageId": "2F5B3C281B283D4EAC694B7425288675"
          }
      							

What to do next

Debug a function