All Products
Search
Document Center

Function Compute:Configure a CDN trigger

Last Updated:Apr 01, 2026

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

ScenarioWhat to do
Log processingArchive or analyze CDN offline log files as soon as they are generated.
Cache managementRun custom logic when cached objects are refreshed or pre-pushed to edge nodes.
Domain lifecycleTrigger 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:

Step 1: Create a CDN trigger

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

  2. In the top navigation bar, select a region. On the Functions page, click the function you want to manage.

  3. On the function details page, click the Triggers tab, then click Create Trigger.

  4. 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.
    ParameterWhat to enterExample
    Trigger typeSelect CDN.CDN
    NameEnter a name for the trigger.cdn-trigger
    Version or aliasThe 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 eventSelect the CDN event type that should invoke the function. See CDN event definitions for a description of each type.CdnDomainStarted
    Triggering event versionLeave as 1.0.0. This is the only supported version.1.0.0
    Domain nameEnter the CDN-accelerated domain name.bucket*******.oss-cn-chengdu-internal.aliyuncs.com
    Description(Optional) Enter a description for the trigger.cdntrigger
    Role nameSelect 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.

  1. On the Code tab of the function details page, click the image.png icon next to Test Function and select Configure Test Parameters from the drop-down list.

  2. 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.

FieldTypeDescription
eventNameStringThe CDN event type, such as LogFileCreated or CdnDomainStarted.
eventSourceStringAlways cdn.
regionStringThe region where CDN is located, such as cn-hangzhou.
eventVersionStringThe event schema version. Always 1.0.0.
eventTimeStringThe time the event occurred, in ISO 8601 format.
traceIdStringA trace ID from CDN, used for troubleshooting.
userIdentity.aliUidStringThe Alibaba Cloud account ID.
resource.domainStringThe CDN-accelerated domain name that generated the event.
eventParameterMapEvent-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"
            }
        }
    ]
}
FieldTypeDescription
domainStringThe domain name.
startTimeStringThe start time of the log file (Unix timestamp).
endTimeStringThe end time of the log file (Unix timestamp).
fileSizeStringThe size of the log file, in bytes.
filePathStringThe 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****"
            }
        }
    ]
}
FieldTypeDescription
domainStringThe domain name.
taskIdStringThe ID of the resource purge task.
objectTypeStringThe purge type: File or Directory.
objectPathStringThe resource identifier.
createTimeStringStart time of the refresh (Unix timestamp).
completeTimeStringThe 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****"
            }
        }
    ]
}
FieldTypeDescription
domainStringThe domain name.
statusStringThe 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****"
            }
        }
    ]
}
FieldTypeDescription
domainStringThe 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.

  1. 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}"
  2. 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:

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