After the Alibaba Cloud Content Delivery Network trigger is configured, whenever the Alibaba Cloud Content Delivery Network service generates an offline log file under the specified accelerated domain name, the Alibaba Cloud Content Delivery Network event triggers the Function Compute to transfer the Alibaba Cloud Content Delivery Network offline log. This topic describes how to configure Alibaba Cloud Content Delivery Network event triggers in the Function Compute console, including creating triggers, writing functions, and testing functions.

Scenarios

Alibaba Cloud CDN supported origin types include OSS domain name, IP address, origin domain name, and Function Compute domain name. Each origin type supports configuring multiple origin addresses. In multi-origin scenarios, you can set the primary /secondary priority and weight of the origin to implement SLB. In this topic, OSS is used as the origin server. You need to create a bucket in the OSS console, obtain the domain name as the origin server, and then add an accelerated domain name for the origin server. To change the origin type, see Configure an origin server.

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 the service resides.
  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 relevant information. Click OK.
    Create Trigger Panel-Alibaba Cloud Content Delivery Network
    Parameter Configuration method Example
    Trigger Type Select Alibaba Cloud Content Delivery Network. CDN
    Parameter Enter a custom trigger name. cdn-trigger
    Version or alias The default value is LATEST. If you want to create a trigger of another version or alias, you must select this version from the Version or Alias drop-down list on the function details page. For more information about the version, see Introduction to versions. LATEST
    Event Name Select a trigger event type.

    For more information about how to select an event type, see CDN events.

    CdnDomainStarted
    The version of the event. The version number of the triggered event.
    Note Currently, only event version 1.0.0 is supported.
    1.0.0
    Domain name Enter the accelerated domain name. bucket-zh****.oss-cn-chengdu-internal.aliyuncs.com
    Description Enter a description for the domain name. cdntrigger
    Role Name Select AliyunCDNEventNotificationRole.
    Note If you create a trigger of this type for the first time, click OK. In the dialog box that appears, select Authorize Now.
    AliyunCDNEventNotificationRole

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

    cdn-trigger

Write code for a function

After you create a Alibaba Cloud Content Delivery Network event trigger, you can start writing function code to transfer Alibaba Cloud Content Delivery Network offline logs Alibaba Cloud Content Delivery Network the event trigger Function Compute.

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.

import json
import oss2
import requests

def handler(event, context):
    creds = context.credentials
    auth = oss2.StsAuth(creds.access_key_id, creds.access_key_secret, creds.security_token)
    bucket = oss2.Bucket(auth, 'your endpoint', 'your bucket name')

  # Parse the event to get the source object info.
  eventObj = json.loads(event)["events"]
  fileUrl = eventObj[0]["eventParameter"]["filePath"]
  file = requests.get(fileUrl)
  result = bucket.put_object("your log name", fileUrl)

  return result.status                       

Test a function

After you define a function, you must debug it to verify the correctness of its code. Actually, when an Alibaba Cloud Content Delivery Network (CDN) event occurs, a CDN event trigger automatically triggers the execution of the function.

  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.

    The event parameter is an input parameter of Function Compute. The following code shows the specific event formats. The key-value pairs in eventParameter vary with CDN event types.

    • Example of the LogFileCreated event

      Replace filePath with the path of your CDN log 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-e4ea39686738",
                  "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 explains the parameters involved in the event.
      Filed Type Example Description
      eventName String LogFileCreated The type of the event involved.
      eventSource String cdn The name of the event source.
      region String cn-hangzhou The region where the Alibaba Cloud Content Delivery Network 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-e4ea39686738 The ID passed by the event source for troubleshooting.
      userIdentity String The attributes of the user.
      aliUid String 164901546557**** The ID of your Alibaba Cloud account.
      resource String The information of the resources.
      domain String example.com The domain names that correspond to the IP addresses that were blocked or unblocked.
      eventParameter String The parameters of the event.
      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.
    • Examples of CachedObjectsRefreshed, CachedObjectsPushed, and CachedObjectsBlocked 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-4c3d08e25acf",
                  "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 explains the parameters involved in the event.
      Filed Type Example Description
      eventName String CachedObjectsRefreshed The type of the event involved.
      eventSource String cdn The name of the event source.
      region String cn-hangzhou The region where the Alibaba Cloud Content Delivery Network 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-e4ea39686738 The ID passed by the event source for troubleshooting.
      resource String The information of the resources.
      domain String example.com The domain names that correspond to the IP addresses that were blocked or unblocked.
      eventParameter String The parameters of the event.
      objectPath String /2018/03/16/13/33b430c57e7.mp4 The identifier of the resource.
      createTime String 1521180769 The start time of the refresh.
      completeTime String 1521180777 The end time of the refresh.
      objectType String File The refresh type. Valid values:
      • File: File
      • Directory: Folder
      taskId String 2089687230 The ID of the resource refresh task.
      userIdentity String The attributes of the user.
      aliUid String 164901546557**** The ID of your Alibaba Cloud account.
    • 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-4c3d08e25acf",
               "resource": {
                    "domain": "example.com" 
               }, 
               "eventParameter": {
                    "domain": "example.com", 
                    "status": "online"
               }, 
               "userIdentity": {
                    "aliUid": "164901546557****"
               } 
            } 
         ]
      }
      The following table explains the parameters involved in the event.
      Filed Type Example Description
      eventName String CachedObjectsRefreshed The type of the event involved.
      eventSource String cdn The name of the event source.
      region String cn-hangzhou The region where the Alibaba Cloud Content Delivery Network 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-e4ea39686738 The ID passed by the event source for troubleshooting.
      resource String The information of the resources.
      domain String example.com The domain names that correspond to the IP addresses that were blocked or unblocked.
      eventParameter String The parameters of the event.
      status String online The status of the accelerated domain name.
      userIdentity String The attributes of the user.
      aliUid String 164901546557**** The ID of your Alibaba Cloud account.
    • Samples of CdnDomainAdded and CdnDomainDeleted 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-4c3d08e25acf",
               "resource": {
                    "domain": "example.com"
               },
               "eventParameter": {
                    "domain": "example.com"
               },
               "userIdentity": {
                    "aliUid": "164901546557****"
               }
            }
         ]
      }
      The following table explains the parameters involved in the event.
      Filed Type Example Description
      eventName String CachedObjectsRefreshed The type of the event involved.
      eventSource String cdn The name of the event source.
      region String cn-hangzhou The region where the Alibaba Cloud Content Delivery Network 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-e4ea39686738 The ID passed by the event source for troubleshooting.
      resource String The information of the resources.
      domain String example.com The domain names that correspond to the IP addresses that were blocked or unblocked.
      eventParameter String The parameters of the event.
      domain String example.com The domain names that correspond to the IP addresses that were blocked or unblocked.
      userIdentity String The attributes of the user.
      aliUid String 164901546557**** The ID of your Alibaba Cloud account.
  3. On the function details page, click the Function Code tab, and then click Test Function.

Check the result

On the Function Code tab, you can view a successful command.

result

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.