Configure a CDN trigger

Updated at:

Configure a CDN trigger for a target function, set the entry parameters, and write and test the function code in the Function Compute console. Each offline log file that CDN then generates under the specified accelerated domain name triggers Function Compute to transfer and store the logs.

Example scenario

Content Delivery Network (CDN) supports the following origin types: Object Storage Service (OSS) domain names, IP addresses, origin domain names, and Function Compute domain names. Each origin type supports configuring multiple addresses of the origin server. In multi-origin scenarios, you can set the primary and secondary priority and weight for origins to achieve load balancing.

This topic uses OSS as the origin server. Create a bucket in the OSS console , get its domain name to use as the origin, and then add an accelerated domain name for the origin. To change the origin type, see Configure an origin.

Prerequisites

Step 1: Create a CDN 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 Trigger tab, and then click Create Trigger.

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

    Parameter

    Description

    Example in this topic

    Trigger type

    Select CDN.

    CDN

    Name

    Enter a custom trigger name.

    cdn-trigger

    Version or alias

    The default value is LATEST. To create a trigger for a different version or alias, you must first select the version from the Version or Alias drop-down list on the function details page. For more information about versions and aliases, see Manage versions and Manage aliases.

    LATEST

    Trigger event

    Select a trigger event type.

    For information about how to select a trigger event type, see Overview of CDN event triggers.

    CdnDomainStarted

    Trigger event version

    The version number of the trigger event.

    Note

    Only event version 1.0.0 is supported.

    1.0.0

    Domain name

    Enter the accelerated domain name.

    bucket*******.oss-cn-chengdu-internal.aliyuncs.com

    Description

    Enter the description of the domain name.

    cdntrigger

    Role name

    Select AliyunCDNEventNotificationRole.

    Note

    If this is the first time you create a trigger of this type, click OK. In the dialog box that appears, select Authorize Now.

    AliyunCDNEventNotificationRole

    After the trigger is created, it is displayed on the Triggers tab. To modify or delete a trigger, see Trigger Management.

Step 2: Configure the entry 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.

    event is the entry parameter of Function Compute. For different CDN event types, the key-value pairs in eventParameter are different.

    • The following example shows the event for LogFileCreated:

      Replace filePath with the path to your CDN logs or any test file.

      {
          "events": [
              {
                  "eventName": "LogFileCreated",
                  "eventSource": "cdn",
                  "region": "cn-hangzhou",
                  "eventVersion": "1.0.0",
                  "eventTime": "2018-06-14T15:31:49+08:00",
                  "traceId": "c6459282-6a4d-4413-894c-e4ea3968****",
                  "userIdentity": {
                      "aliUid": "164901546557****"
                  },
                  "resource": {
                      "domain": "example.com"
                  },
                  "eventParameter": {
                      "domain": "example.com",
                      "endTime": 1528959900,
                      "fileSize": 1788115,
                      "filePath": "http://cdnlog.cn-hangzhou.oss.aliyun-inc.com/www.aliyun.com/2017_12_27/www.aliyun.com_2017_12_27_0800_0900.gz?OSSAccessKeyId=xxxx&Expires=xxxx&Signature=xxxx",
                      "startTime": 1528959600
                  }
              }
          ]
      }
                          

      The following table describes the different attribute fields in the event parameter.

      Parameter

      Type

      Example value

      Description

      eventName

      String

      LogFileCreated

      The event type.

      eventSource

      String

      cdn

      The name of the event source.

      region

      String

      cn-hangzhou

      The region where CDN is located.

      eventVersion

      String

      1.0.0

      The event trigger version.

      eventTime

      String

      2018-06-14T15:31:49+08:00

      The time when the event occurred.

      traceId

      String

      c6459282-6a4d-4413-894c-e4ea3968****

      The ID passed by the event source, used for troubleshooting.

      userIdentity

      Map

      The user attributes.

      aliUid

      String

      164901546557****

      The Alibaba Cloud account ID.

      resource

      Map

      The resource information.

      domain

      String

      example.com

      The domain name.

      eventParameter

      Map

      The event parameters.

      domain

      String

      example.com

      The domain name.

      endTime

      String

      1528959900

      The end time of the log file.

      fileSize

      String

      1788115

      The size of the log file.

      filePath

      String

      http://cdnlog.cn-hangzhou.oss.aliyun-inc.com/www.aliyun.com/2017_12_27/www.aliyun.com_2017_12_27_0800_0900.gz?OSSAccessKeyId=xxxx&Expires=xxxx&Signature=xxxx

      The address of the log file.

      startTime

      String

      1528959600

      The start time of the log file.

    • The following example shows the events for CachedObjectsRefreshed and CachedObjectsPushed:

      {
          "events": [
              {
                  "eventName": "CachedObjectsRefreshed",
                  "eventVersion": "1.0.0", 
                  "eventSource": "cdn", 
                  "region": "cn-hangzhou",
                  "eventTime": "2018-03-16T14:19:55+08:00",
                  "traceId": "cf89e5a8-7d59-4bb5-a33e-4c3d08e2****",
                  "resource": {
                      "domain": "example.com"
                  },
                  "eventParameter": {
                      "objectPath": [
                          "/2018/03/16/13/33b430c57e7.mp4",
                          "/2018/03/16/14/4ff6b9bd54d.mp4"
                      ],
                      "createTime": 1521180769,
                      "domain": "example.com",
                      "completeTime": 1521180777,
                      "objectType": "File",
                      "taskId": 2089687230
                  },
                  "userIdentity": {
                      "aliUid": "164901546557****"
                  }
              }
          ]
      }                    

      The following table describes the different attribute fields in the event parameter.

      Parameter

      Type

      Example value

      Description

      eventName

      String

      CachedObjectsRefreshed

      The event type.

      eventSource

      String

      cdn

      The name of the event source.

      region

      String

      cn-hangzhou

      The region where CDN is located.

      eventVersion

      String

      1.0.0

      The event trigger version.

      eventTime

      String

      2018-06-14T15:31:49+08:00

      The time when the event occurred.

      traceId

      String

      c6459282-6a4d-4413-894c-e4ea3968****

      The ID passed by the event source, used for troubleshooting.

      resource

      Map

      The resource information.

      domain

      String

      example.com

      The domain name.

      eventParameter

      Map

      The event parameters.

      objectPath

      String

      /2018/03/16/13/33b430c57e7.mp4

      The resource identifier.

      createTime

      String

      1521180769

      The time when the refresh started.

      domain

      String

      example.com

      The domain name.

      completeTime

      String

      1521180777

      The time when the refresh ended.

      objectType

      String

      File

      The refresh type. Valid values:

      • File: files.

      • Directory: directories.

      taskId

      String

      2089687230

      The ID of the resource refresh task.

      userIdentity

      Map

      The user attributes.

      aliUid

      String

      164901546557****

      The Alibaba Cloud account ID.

    • The following example shows the events for CdnDomainStarted and CdnDomainStopped:

      {  "events": [
            { 
               "eventName": "CdnDomainStarted",
               "eventVersion": "1.0.0",
               "eventSource": "cdn",
               "region": "cn-hangzhou",
               "eventTime": "2018-03-16T14:19:55+08:00",
               "traceId": "cf89e5a8-7d59-4bb5-a33e-4c3d08e2****",
               "resource": {
                    "domain": "example.com" 
               }, 
               "eventParameter": {
                    "domain": "example.com", 
                    "status": "online"
               }, 
               "userIdentity": {
                    "aliUid": "164901546557****"
               } 
            } 
         ]
      }

      The following table describes the different attribute fields in the event parameter.

      Parameter

      Type

      Example value

      Description

      eventName

      String

      CdnDomainStarted

      The event type.

      eventSource

      String

      cdn

      The name of the event source.

      region

      String

      cn-hangzhou

      The region where CDN is located.

      eventVersion

      String

      1.0.0

      The event trigger version.

      eventTime

      String

      2018-06-14T15:31:49+08:00

      The time when the event occurred.

      traceId

      String

      c6459282-6a4d-4413-894c-e4ea3968****

      The ID passed by the event source, used for troubleshooting.

      resource

      Map

      The resource information.

      domain

      String

      example.com

      The domain name.

      eventParameter

      Map

      The event parameters.

      domain

      String

      example.com

      The domain name.

      status

      String

      online

      The status of the domain name.

      userIdentity

      Map

      The user attributes.

      aliUid

      String

      164901546557****

      The Alibaba Cloud account ID.

    • The following example shows the events for CdnDomainAdded and CdnDomainDeleted:

      {  "events": [
            {
               "eventName": "CdnDomainAdded",
               "eventVersion": "1.0.0",
               "eventSource": "cdn",
               "region": "cn-hangzhou",
               "eventTime": "2018-03-16T14:19:55+08:00",
               "traceId": "cf89e5a8-7d59-4bb5-a33e-4c3d08e2****",
               "resource": {
                    "domain": "example.com"
               },
               "eventParameter": {
                    "domain": "example.com"
               },
               "userIdentity": {
                    "aliUid": "164901546557****"
               }
            }
         ]
      }

      The following table describes the different attribute fields in the event parameter.

      Parameter

      Type

      Example value

      Description

      eventName

      String

      CdnDomainAdded

      The event type.

      eventSource

      String

      cdn

      The name of the event source.

      region

      String

      cn-hangzhou

      The region where CDN is located.

      eventVersion

      String

      1.0.0

      The event trigger version.

      eventTime

      String

      2018-06-14T15:31:49+08:00

      The time when the event occurred.

      traceId

      String

      c6459282-6a4d-4413-894c-e4ea3968****

      The ID passed by the event source, used for troubleshooting.

      resource

      Map

      The resource information.

      domain

      String

      example.com

      The domain name.

      eventParameter

      Map

      The event parameters.

      domain

      String

      example.com

      The domain name.

      userIdentity

      Map

      The user attributes.

      aliUid

      String

      164901546557****

      The Alibaba Cloud account ID.

Step 3: Write and test the function code

After you create the CDN event trigger, you can write the function code and test it to verify its correctness. In production, the trigger automatically invokes the function when a CDN event occurs.

  1. On the Code tab of the function details page, write code in the code editor and click Deploy Code.

    This topic uses Python function code as an example.

    # -*- coding: utf-8 -*-
    import logging
    import json
    
    logger = logging.getLogger("cdn-sample")
    
    def handler(event, context):
        evt = json.loads(event)
        eventObj = evt["events"][0]
        eventName = eventObj['eventName']
        info = ""
        eventParam = eventObj['eventParameter']
        domain = eventParam['domain']
        if eventName == "CachedObjectsRefreshed" or eventName == "CachedObjectsPushed":
            objPathList = eventParam['objectPath']
            info = ",".join(objPathList)
        elif eventName == "LogFileCreated":
            info = eventParam['filePath']
        elif eventName == "CdnDomainStarted" or eventName == "CdnDomainStopped":
            # Corresponding business logic
            pass
        elif eventName == "CdnDomainAdded" or eventName == "CdnDomainDeleted":
            # Corresponding business logic
            pass
        return f"eventName:{eventName}, domain: {domain}, info: {info}"   
  2. Click Test Function.

    After the execution is complete, you can view the execution result above the Code tab.

More information

In addition to the Function Compute console, you can configure triggers by using one of the following methods:

  • Use Serverless Devs tool to configure triggers. For more operations, please refer to Common Serverless Devs commands.

  • Use SDKs to configure triggers. For more information, see SDKs.

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