Use Function Compute to automatically and periodically archive offline logs from Alibaba Cloud CDN to Object Storage Service (OSS) for long-term retention and analysis.
Background
Alibaba Cloud CDN provides detailed access logs for your accelerated domain names. These offline logs are essential for user behavior analysis, service troubleshooting, and operational data statistics. Alibaba Cloud CDN retains offline log files on its servers for only 30 days before automatically deleting them.
To meet requirements for data compliance, long-term audits, or historical data analysis, you may need to store these logs permanently. Object Storage Service (OSS) provides a highly available, cost-effective, and durable storage solution, making it an ideal choice for long-term log archiving. Function Compute listens for events generated when Alibaba Cloud CDN creates logs and then invokes a function to archive the offline logs to Object Storage Service (OSS). This solution creates an automated workflow that seamlessly archives Alibaba Cloud CDN logs to your OSS bucket.
How it works
This automated archiving solution uses Function Compute as a "scheduler" and "data mover" to connect Alibaba Cloud CDN and Object Storage Service (OSS). The workflow is as follows:
-
Event trigger: A Function Compute trigger fires whenever Alibaba Cloud CDN generates a new log file.
-
Function execution: When the trigger fires, it automatically executes the associated function code.
-
Log retrieval: The function code calculates the previous day's log file name based on the current date and generates a download URL for the Alibaba Cloud CDN offline log. It then sends a request to this URL to download the log file to the Function Compute temporary environment.
-
Transfer to Object Storage Service (OSS): After the function successfully downloads the log file, it calls the OSS API to upload the file to a specified directory in your designated OSS bucket.
The entire process is fully automated, tightly integrating Alibaba Cloud CDN, Function Compute, and Object Storage Service (OSS) to streamline your cloud service management.
Billing
This solution involves charges for the following products:
-
Alibaba Cloud CDN: Generating and downloading offline logs is free of charge.
-
Function Compute: You are charged based on the number of function invocations, resources consumed (vCPU and memory), and execution duration. For a lightweight log-archiving task that runs only a few times a day, the costs are typically minimal. For more information, see Function Compute Billing Overview.
-
Object Storage Service (OSS): You are charged based on the storage space you use, the number of API requests, and any outbound internet traffic. For more information, see Object Storage Service (OSS) Billing Overview.
Prerequisites
-
Make sure that you have activated Alibaba Cloud CDN, Function Compute, and Object Storage Service (OSS) under the same Alibaba Cloud account. This ensures smooth authorization and access between services.
-
Create an Object Storage Service (OSS) bucket to store the log files. For more information, see Create buckets. Record the bucket name, the public access endpoint, and the log storage directory name.
Procedure
1. Obtain bucket details
The Function Compute function requires your OSS bucket details. Follow these steps to obtain the bucket name, the public access endpoint, and the log storage directory name.
2. Create the Function Compute function and trigger
In Function Compute, you will create a function to perform the archiving and a trigger to start it.
-
Log on to the Function Compute console. In the left-side navigation pane, choose Functions.
-
On the Functions page, click Create Function, select Event Function, and then click Create Event Function.
-
When creating the event function, configure the following key parameters:
-
Basic Configuration-Function Name: This function name is required for subsequent operations. We recommend that you use
cdn-log-dump. -
Function Code > Runtime: The function is written in Python. Select Official Runtimes, Python, and then Python 3.10.
-
Advanced Settings > Environment Variable: To pass the OSS bucket details to the function, create the following environment variables:
-
target_oss_bucket: Bucket name -
target_oss_endpoint: The endpoint for public access -
target_oss_prefix: The name of the directory for storing log files
For example, set target_oss_bucket to
demo-bucket, target_oss_endpoint tooss-cn-hangzhou.aliyuncs.com, and target_oss_prefix tocdn_log. -
-
-
After you configure the parameters, click Create.
-
In Function Details, click the Triggers tab, and then click Create Trigger.
-
Configure the key parameters for the trigger as follows, and then click OK.
-
Trigger Type: Select Alibaba Cloud CDN (Sync invocation).
-
Name: Enter a name for the trigger (Recommended:
cdn-logs-triggers). -
Event Name: Select LogFileCreated.
-
Domain Name: Enter an active accelerated domain name that is under the same Alibaba Cloud account.
-
Description: Enter a description for the trigger. We recommend that you use "CDN offline log file generation trigger".
-
Role: Select AliyunCDNEventNotificationRole.
-
-
After you configure the trigger parameters, click OK. If the No Default Role Is Created for CDN Trigger message appears, click Authorize Now and follow the on-screen instructions to create the default role. Otherwise, the trigger is created immediately.
-
In Function Details, click the Code tab, and in the online editor, enter the code below, which retrieves offline logs from Alibaba Cloud CDN and stores them in Object Storage Service (OSS).
-
Click Deploy Code.
3. Create a custom role and policy
The Function Compute function needs permissions to access Object Storage Service (OSS). You can grant these permissions by attaching a custom role to the function. Follow these steps to create the required role and policy.
-
Log on to the Resource Access Management (RAM) console. In the left-side navigation pane, choose Permissions > Policies.
-
Click Create Policy and select the JSON tab.
-
In the following policy, replace
BucketNamewith your bucket name, and replace all three occurrences ofFC-NAMEwith the function name from Step 2 (we recommend that you usecdn-log-dump).{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": "oss:PutObject", "Resource": "acs:oss:*:*:BucketName/*" }, { "Effect": "Allow", "Action": "fc:InvokeFunction", "Resource": [ "acs:fc:*:*:services/FC-NAME/functions/FC-NAME", "acs:fc:*:*:services/FC-NAME.*/functions/*" ] } ] } -
Click OK and enter a Policy Name and a Remarks. Then, click OK again to create the policy (We recommend that you set Policy Name to
AliyunCDNLogDumpAccessand Description to Permissions to manage CDN offline log dumps). -
In the left-side navigation pane, choose Identities > Roles and click Create Role.
-
For Select Trusted Entity, select Alibaba Cloud Service. For Trusted Service, select Function Compute, and then click OK.
-
In the Create Role dialog box, enter a Role Name (we recommend that you use
AliyunCDNLogDumpRole) and click OK to create the role. -
On the Permissions tab, click Precise Authorization. For Policy Type, select Custom Policies, and for Policy Name, enter the name of the policy that you created in Step 4 (we recommend that you use
AliyunCDNLogDumpAccess). Then, click OK. -
On the Trust Policy tab, click Edit Trust Policy. In the JSON editor, enter the following trust policy, and then click OK.
{ "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": [ "fc.aliyuncs.com" ] } } ], "Version": "1" }
4. Attach the role to the function
-
On the Functions tab in the Function Compute console, select the function that you created in Step 2, and click Configure.
-
On the function details page, click the Configure tab. In the Advanced Settings section, click Configure.
-
In Advanced Settings, find the Permissions - Function Role option and select the role that you created in Step 3. We recommend that you use
AliyunCDNLogDumpRole. Then, click Deploy to bind the role to the Function Compute task.
5. Test the function (Optional)
The configuration is now complete. Because Alibaba Cloud CDN generates offline logs with a delay of about 24 hours, you cannot immediately verify that the setup works. To test the function right away, follow these optional steps.
-
On the Functions tab of the Function Compute console, select the function created in Step 2, and click Configure.
-
On the function details page, click the Test tab. For Test Event, select Create New Test Event. For Event Template, select CDN (LogFileCreated). For Event Name, enter Test_cdn_log_dump.
The JSON editor is automatically populated with the CDN LogFileCreated event template, which includes fields such as
eventName,eventSource,region,eventTime,traceId,userIdentity(aliUid),resource(for example, the domain is example.com), andeventParameter. -
Use the parameters obtained below to modify the
filePathparameter in the event template. -
Click Test Function. After the execution is complete, the returned result is
nulland the execution status is successful. -
In the Object Storage Service (OSS) console, select the bucket that is used to store the Alibaba Cloud CDN logs.
-
Click Files and navigate to the directory that you configured to store the Alibaba Cloud CDN logs. You will see a folder named after the accelerated domain name. Inside it, a subfolder named after the date contains the archived log file. This confirms that the Function Compute task ran successfully.