A CDN trigger invokes your function each time Alibaba Cloud CDN generates an offline log file for a specified accelerated domain name. Use CDN triggers to automate log processing, cache management, and domain lifecycle workflows without polling or manual intervention.
Use cases
| Scenario | What to do |
|---|---|
| Log processing | Archive or analyze CDN offline log files as soon as they are generated. |
| Cache management | Run custom logic when cached objects are refreshed or pre-pushed to edge nodes. |
| Domain lifecycle | Trigger workflows when a CDN-accelerated domain is added, removed, started, or stopped. |
Example scenario
This guide uses Object Storage Service (OSS) as the origin server. Create a bucket in the OSS console, get its domain name to use as the origin server, then add an accelerated domain name for that origin. For instructions on changing the origin server type, see Configure an origin server.
Alibaba Cloud CDN supports multiple origin server types: OSS domain names, IP addresses, origin domain names, and Function Compute domain names. In multi-origin scenarios, assign primary/secondary priority and weight values to balance load across origin servers.
Prerequisites
Before you begin, ensure that you have:
A Function Compute function (Create a function)
An OSS bucket (Create a bucket)
A CDN-accelerated domain name (Add an accelerated domain name)
Step 1: Create a CDN trigger
Log in 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 you want to manage.
On the function details page, click the Triggers tab, then click Create Trigger.
In the Create Trigger panel, configure the following parameters and click OK.
If this is the first time you create a CDN trigger, click OK in the panel, then click Authorize Now in the dialog that appears. This grants CDN permission to invoke your function.
Parameter What to enter Example Trigger type Select CDN. CDN Name Enter a name for the trigger. cdn-triggerVersion or alias The default is LATEST. To bind the trigger to a specific version or alias, first select it from the Version Or Alias drop-down list on the function details page. See Manage versions and Manage aliases. LATEST Triggering event Select the CDN event type that should invoke the function. See CDN event definitions for a description of each type. CdnDomainStartedTriggering event version Leave as 1.0.0. This is the only supported version.1.0.0Domain name Enter the CDN-accelerated domain name. bucket*******.oss-cn-chengdu-internal.aliyuncs.comDescription (Optional) Enter a description for the trigger. cdntriggerRole name Select AliyunCDNEventNotificationRole. AliyunCDNEventNotificationRole
After the trigger is created, it appears on the Triggers tab. To modify or delete it, see Manage triggers.
Step 2: Configure test parameters
Set up a test event to verify that your function handles CDN events correctly.
On the Code tab of the function details page, click the
icon next to 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 an event name and content, then click OK.
All CDN trigger events share the same top-level structure. Only the eventParameter field varies by event type.
Shared event fields
The following fields appear in all CDN event types.
| Field | Type | Description |
|---|---|---|
eventName | String | The CDN event type, such as LogFileCreated or CdnDomainStarted. |
eventSource | String | Always cdn. |
region | String | The region where CDN is located, such as cn-hangzhou. |
eventVersion | String | The event schema version. Always 1.0.0. |
eventTime | String | The time the event occurred, in ISO 8601 format. |
traceId | String | A trace ID from CDN, used for troubleshooting. |
userIdentity.aliUid | String | The Alibaba Cloud account ID. |
resource.domain | String | The CDN-accelerated domain name that generated the event. |
eventParameter | Map | Event-specific fields. See the tables below. |
eventParameter fields by event type
`LogFileCreated` — triggered when CDN generates an offline log file
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",
"startTime": 1528959600,
"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"
}
}
]
}| Field | Type | Description |
|---|---|---|
domain | String | The domain name. |
startTime | String | The start time of the log file (Unix timestamp). |
endTime | String | The end time of the log file (Unix timestamp). |
fileSize | String | The size of the log file, in bytes. |
filePath | String | The download URL of the log file. This is the main field your function processes. |
`CachedObjectsRefreshed` / `CachedObjectsPushed` — triggered on cache refresh or pre-push
{
"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": {
"domain": "example.com",
"taskId": 2089687230,
"objectType": "File",
"objectPath": [
"/2018/03/16/13/33b430c57e7.mp4",
"/2018/03/16/14/4ff6b9bd54d.mp4"
],
"createTime": 1521180769,
"completeTime": 1521180777
},
"userIdentity": {
"aliUid": "164901546557****"
}
}
]
}| Field | Type | Description |
|---|---|---|
domain | String | The domain name. |
taskId | String | The ID of the resource purge task. |
objectType | String | The purge type: File or Directory. |
objectPath | String | The resource identifier. |
createTime | String | Start time of the refresh (Unix timestamp). |
completeTime | String | The time the task completed (Unix timestamp). |
`CdnDomainStarted` / `CdnDomainStopped` — triggered when a domain is enabled or disabled
{
"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****"
}
}
]
}| Field | Type | Description |
|---|---|---|
domain | String | The domain name. |
status | String | The domain status: online or offline. |
`CdnDomainAdded` / `CdnDomainDeleted` — triggered when a domain is added or removed
{
"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****"
}
}
]
}| Field | Type | Description |
|---|---|---|
domain | String | The domain name that was added or deleted. |
Step 3: Write and test the function code
After creating the trigger, write the function code and verify it handles each event type correctly. In production, the trigger invokes the function automatically when a CDN event occurs.
On the Code tab, write the code in the editor and click Deploy Code. The following Python example shows how to handle each CDN event type:
# -*- 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 execution completes, the result appears above the Code tab.
What's next
In addition to the Function Compute console, configure triggers using:
Serverless Devs: See Common commands of Serverless Devs.
SDKs: See SDKs.
To modify or delete an existing trigger, see Manage triggers.