After you configure an Alibaba Cloud CDN trigger, every time Alibaba Cloud CDN generates an offline log file for a specified domain name, an Alibaba Cloud CDN event triggers a function in Function Compute to dump the Alibaba Cloud CDN offline logs. This topic describes how to configure a CDN trigger for a function in the Function Compute console, including how to create a CDN trigger, configure input parameters, write function code, and test the function.
Scenario
Alibaba Cloud CDN supports the following types of origin servers: Object Storage Service (OSS) buckets (domain names), self-managed origin servers (IP addresses and domain names), 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 bucket is used as the origin server. You need to create a bucket in the OSS console, obtain its domain name as the origin server address, and then add a domain name to be accelerated by Alibaba Cloud CDN. To change the type of the origin server, see Configure an origin server.
Prerequisites
- Function Compute
- OSS
- CDN
Step 1: Create a CDN trigger
- Log on to the Function Compute console.
- In the left-side navigation pane, click Services and Functions.
- In the top navigation bar, select the region where the service resides.
- On the Services page, find the desired service and click Functions in the Actions column.
- On the Functions page, click the function that you want to manage.
- 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.
- In the Create Trigger panel, specify related parameters. After you specify the parameters,
click OK.
Field Description Example Trigger Type Select Alibaba Cloud CDN. CDN Name Enter a custom trigger name. cdn-trigger Version or Alias The version or alias of the service. 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 of a service, 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 CDN events.
CdnDomainStarted Trigger Event Version 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
- On the function details page, click the Code tab and click the
icon. From the drop-down list that appears, select Configure Test Parameters.
- In the Configure Test Parameters panel, click the Create New Test Event or Modify Existing Test Event tab, and specify Event Name and the event content. After you specify the parameters, click OK.
The event parameter is an input parameter of Function Compute. The following code shows the format of the event parameter. 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 the Alibaba Cloud CDN log 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 user attributes. aliUid String 164901546557**** The ID of the Alibaba Cloud account. resource Map The information about the resource. domain String example.com The domain name. eventParameter Map The parameters of the event. 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 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-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 resource. domain String example.com The domain name. eventParameter Map 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. 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 user attributes. aliUid String 164901546557**** The ID of the 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-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 resource. domain String example.com The domain name. eventParameter Map The parameters of the event. 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 ID of the Alibaba Cloud account. - 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 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 resource. domain String example.com The domain name. eventParameter Map The parameters of the event. domain String example.com The domain name. userIdentity Map The user attributes. aliUid String 164901546557**** The ID of the Alibaba Cloud account.
- Example of the LogFileCreated event
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.
- On the function details page, click the Code tab, enter function code in the code editor, and then click Save.
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
- Click the Code tab and click Test Function.
After the function is executed, you can view the result on the Code tab.
References
- Use Serverless Devs to configure triggers. For more information, see Serverless Devs.
- Use SDKs to configure triggers. For more information, see Supported SDKs.
To modify or delete an existing trigger, see Manage triggers.