All Products
Search
Document Center

Function Compute:Configure an Alibaba Cloud CDN trigger

Last Updated:Dec 12, 2023

After you configure an Alibaba Cloud CDN trigger, each time Alibaba Cloud CDN generates an offline log file under the specified accelerated domain name, the Alibaba Cloud CDN event triggers Function Compute to store the offline logs of Alibaba Cloud CDN. This topic describes how to configure an Alibaba Cloud CDN trigger for a function in the Function Compute console, including how to create a trigger, configure input parameters, and write and test function code.

Example scenario

Alibaba Cloud CDN supports the following types of origin servers: Object Storage Service (OSS) endpoints, IP addresses of origin servers, domain names of origin servers, and Function Compute domain names. You can specify one or more origin servers of each type and specify primary and secondary origin servers to balance loads.

In this topic, an OSS endpoint is used as the origin server. You need to create a bucket in the OSS console, obtain the endpoint as the origin server, and then add a domain name to Alibaba Cloud CDN. For more information about how to change the origin server, see Configure an origin server.

Before you start

Step 1: Create an Alibaba Cloud CDN trigger

  1. Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.

  2. In the top navigation bar, select a region. On the Services page, find the service that you want to manage and click Functions in the Actions column.

  3. On the Functions page, click the function that you want to modify.

  4. On the function details page, click the Triggers tab, select a version or alias from the Version or Alias drop-down list, and then click Create Trigger.

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

    Parameter

    Description

    Example

    Trigger Type

    Select Alibaba Cloud CDN.

    CDN

    Name

    Enter a trigger name.

    cdn-trigger

    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 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 more information about how to select an event type, see the "CDN events" section in Overview.

    CdnDomainStarted

    Trigger Event Version

    Specify the version of the trigger event.

    Note

    Only event version 1.0.0 is supported.

    1.0.0

    Domain Name

    Enter an accelerated domain name.

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

    Description

    Enter a description about the accelerated domain name.

    cdntrigger

    Role Name

    Select AliyunCDNEventNotificationRole.

    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 message that appears.

    AliyunCDNEventNotificationRole

    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, click the xialatubiao icon next to Test Function, and then select Configure Test Parameters from the drop-down list.

  2. In the Configure Test Parameters panel, select the Create New Test Event or Modify Existing Test Event tab, configure Event Name and the event content, and then click OK.

    The event parameter is an input parameter of Function Compute. The following items show the format of event. The key-value pairs in eventParameter vary with the Alibaba Cloud CDN event type.

    • Example of the LogFileCreated event

      Replace filePath with the path of Alibaba Cloud CDN logs or a 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 fields in the event parameter.

      Field

      Type

      Example

      Description

      eventName

      String

      LogFileCreated

      The type of the event.

      eventSource

      String

      cdn

      The name of the event source.

      region

      String

      cn-hangzhou

      The region where Alibaba Cloud CDN resides. Default value: cn-hangzhou.

      eventVersion

      String

      1.0.0

      The version of the event trigger.

      eventTime

      String

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

      The time at which an event occurred.

      traceId

      String

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

      The ID that is delivered by the event source for troubleshooting.

      userIdentity

      Map

      The information about the user.

      aliUid

      String

      164901546557****

      The ID of the Alibaba Cloud account that created the namespace.

      resource

      Map

      The information about the resources.

      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 location of the log file.

      startTime

      String

      1528959600

      The start time of the log file.

    • Examples of event for the CachedObjectsRefreshed and CachedObjectsPushed events

      {
          "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 fields in the event parameter.

      Field

      Type

      Example

      Description

      eventName

      String

      CachedObjectsRefreshed

      The type of the event.

      eventSource

      String

      cdn

      The name of the event source.

      region

      String

      cn-hangzhou

      The region where Alibaba Cloud CDN resides. Default value: cn-hangzhou.

      eventVersion

      String

      1.0.0

      The version of the event trigger.

      eventTime

      String

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

      The time at which an event occurred.

      traceId

      String

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

      The ID that is delivered by the event source for troubleshooting.

      resource

      Map

      The information about the resources.

      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 start time of the refresh.

      domain

      String

      example.com

      The domain name.

      completeTime

      String

      1521180777

      The end time of the refresh.

      objectType

      String

      File

      The item that is refreshed. Valid values:

      • File: files

      • Directory: folders

      taskId

      String

      2089687230

      The ID of the resource refresh task.

      userIdentity

      Map

      The information about the user.

      aliUid

      String

      164901546557****

      The ID of the Alibaba Cloud account that created the namespace.

    • Samples of CdnDomainStarted and CdnDomainStopped events

      {  "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 fields in the event parameter.

      Field

      Type

      Example

      Description

      eventName

      String

      CdnDomainStarted

      The type of the event.

      eventSource

      String

      cdn

      The name of the event source.

      region

      String

      cn-hangzhou

      The region where Alibaba Cloud CDN resides. Default value: cn-hangzhou.

      eventVersion

      String

      1.0.0

      The version of the event trigger.

      eventTime

      String

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

      The time at which an event occurred.

      traceId

      String

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

      The ID that is delivered by the event source for troubleshooting.

      resource

      Map

      The information about the resources.

      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 accelerated domain name.

      userIdentity

      Map

      The information about the user.

      aliUid

      String

      164901546557****

      The ID of the Alibaba Cloud account that created the namespace.

    • Samples of CdnDomainAdded and CdnDomainDeleted events

      {  "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 fields in the event parameter.

      Field

      Type

      Example

      Description

      eventName

      String

      CdnDomainAdded

      The event type.

      eventSource

      String

      cdn

      The name of the event source.

      region

      String

      cn-hangzhou

      The region where Alibaba Cloud CDN resides. Default value: cn-hangzhou.

      eventVersion

      String

      1.0.0

      The version of the event trigger.

      eventTime

      String

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

      The time at which an event occurred.

      traceId

      String

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

      The ID that is delivered by the event source for troubleshooting.

      resource

      Map

      The information about the resources.

      domain

      String

      example.com

      The domain name.

      eventParameter

      Map

      The event parameters.

      domain

      String

      example.com

      The domain name.

      userIdentity

      Map

      The information about the user.

      aliUid

      String

      164901546557****

      The ID of the Alibaba Cloud account that created the namespace.

Step 3: Write and test the function

After you create a CDN trigger, you can write function code and test the function to verify whether the code is correct. When an Alibaba Cloud CDN event occurs, Alibaba Cloud CDN triggers the function.

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

    This example uses the function code that is written in Python.

    # -*- 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. On the Code tab, click Test Function.

    After the function is run, 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 SDKs.

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