Use Alibaba Cloud Function Compute to automatically and periodically store offline logs from Content Delivery Network (CDN) in Object Storage Service (OSS). This enables long-term log archiving and analysis.
Background information
CDN provides detailed access logs for your accelerated domain names. These offline logs are important for user behavior analysis, service troubleshooting, and analyzing operational data. According to the CDN service policy, offline log files are kept on CDN servers for only 30 days and are automatically deleted after this period.
You may need to save these logs permanently to meet requirements for data compliance, long-term audits, or historical data analysis. Object Storage Service (OSS) provides a high-availability, low-cost, and durable storage solution, making it an ideal choice for long-term log archiving. Function Compute (FC) listens for events that generate CDN logs and invokes a task function to store the offline logs from CDN in OSS. With this solution, you can build an automated workflow to seamlessly store your CDN logs in your OSS bucket.
Workflow
The core of this automated storage solution is to use FC as a scheduler and executor to connect CDN and OSS. The workflow is as follows:
Event trigger: A trigger is configured in Function Compute. The trigger is activated whenever CDN generates a log.
Function execution: When the event trigger is activated, it automatically executes the associated function code.
Pull logs: The function code calculates the log file name for the previous day based on the current date and generates a download URL for the CDN offline log. The function then sends a request to this URL to download the log file to the FC temporary environment.
Store in 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 and integrates three Alibaba Cloud services: CDN, FC, and OSS. This improves the efficiency of cloud service management.
Billing
This solution involves billing for the following products:
CDN: The feature to generate and provide downloads for offline logs is free.
FC: FC is billed based on the number of function executions, resources consumed (vCPU and memory), and execution duration. For lightweight log storage tasks that run only a few times a day, the cost is typically very low. For more information, see Function Compute Billing overview.
OSS: OSS is billed based on the storage space you use, the number of API requests, and any outbound traffic over the internet. For more information, see Object Storage Service (OSS) Billing overview.
Prerequisites
Ensure that your CDN, FC, and OSS are activated under the same Alibaba Cloud account.
Follow the instructions in Create a bucket to create a bucket in OSS to store log files. Record the bucket name, the endpoint value for internet access, and the name of the directory where log files will be stored.
Configuration steps
1. Get bucket configuration
When you create the task function in FC, you must provide OSS information for log storage. Therefore, you must first obtain the bucket name, the endpoint value for internet access, and the name of the directory for storing log files. Follow these steps to retrieve this information:
2. Create a Function Compute task
The core of this automated storage solution is to use FC as a scheduler and executor. Therefore, you must configure the corresponding trigger and task function in FC.
Go to the Function Compute 3.0 console. In the navigation pane on the left, select Functions.
On the Functions tab, click Create Function, select Event Function, and then click Create Event Function.
When you create the event function, configure only the key parameters that affect the proper execution of the function.
Basic Configurations - Function Name: You will use this function name in subsequent steps. We recommend that you use
cdn-log-dump.Code - Runtime: Because the task function is Python code, select Built-in Runtime, Python, and Python 3.10.
More Configurations - Environment Variables: The task function needs to obtain bucket information. Therefore, you must pass the bucket configuration information in the environment variables. Create three environment variables and enter the corresponding parameters:
target_oss_bucket: Bucket Nametarget_oss_endpoint: Endpoint value for internet accesstarget_oss_prefix: Name of the directory for storing log files

After you configure the parameters, click Create to create the function.
On the Function Details page, click the Triggers tab, and then click Create Trigger.

Follow these instructions to configure the key parameters for the trigger. Click OK.
Trigger Type: Select Alibaba Cloud CDN(Sync invocation).
Name: Enter a trigger name. We recommend that you use
cdn-logs-triggers.Triggering Event: Select LogFileCreated.
Domain Name: You must enter a CDN-accelerated domain name that is under the same Alibaba Cloud account and is running properly.
Description: Enter a description for the trigger. We recommend that you use: CDN offline log file creation trigger.
Role Name: Select AliyunCDNEventNotificationRole.
After you configure the trigger parameters, click OK. If the message No Default Role Is Created for CDN Trigger appears, click Authorize Now and follow the instructions to create the default role. If this message does not appear, the trigger is created directly.

On the Function Details page, click the Code tab. In the online compiler, enter the following code to pull offline logs from CDN and store them in OSS.
Click Deploy to complete the function configuration.
3. Create a dedicated role and access policy
FC requires permissions to access OSS. To simplify the authorization process, FC supports role association. Follow these steps to configure a role that allows the offline log storage function to access OSS.
Open the Resource Access Management (RAM) console. In the navigation pane on the left, choose Permissions > Policies.
Click Create Policy. On the Create Policy page, click the JSON tab.
In the following policy, replace
BucketNamewith your bucket name, and replace all three instances 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. Enter a Policy Name and a Description, and then click OK again to complete the Create Policy process. (We recommend using
AliyunCDNLogDumpAccessas the Policy Name andManage permissions for CDN log storageas the Description).In the navigation pane on the left, choose Identities > Roles. On the Roles page, click Create Role.
For Principal Type, select Cloud Account. For Principal Name, select Current Alibaba Cloud Account. Then, click OK.
In the Create Role panel, enter a Role Name. We recommend that you use
AliyunCDNLogDumpRole. Then, click OK to create the role.On the details page of the role, click the Permissions tab, and then click Precise Permission. Set Policy Type to Custom Policy. For Policy Name, enter the name of the policy you created in Step 4. We recommend that you use
AliyunCDNLogDumpAccess. Then, click OK.Click the Trust Policy tab, and then 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" }
At this point, you have completed the role and permission configuration. Next, you need to bind this role to the FC task.
4. Bind the role to the Function Compute task
In the FC console, on the Functions tab, select the function you created in Step 2, and then click Configure.
On the Configurations tab, select Advanced Settings, and then click the corresponding Modify button.

In the Advanced Settings, find the Permissions - Function Role option. Select the role you created in Step 3. We recommend that you use
AliyunCDNLogDumpRole. Then, click Deploy to bind the role to the FC task.
5. Test the Function Compute task (optional)
After you complete the first four steps, the entire process for storing CDN offline logs in OSS is configured. However, because there is a delay of about 24 hours in generating offline logs, you cannot immediately verify that the configured FC task is running correctly. You can test the configured FC task by following these steps.
In the FC console, on the Functions tab, select the function you created in Step 2, and then click Configure.
On the Tests tab, for Test Event, select Create New Test Event. For Event Templates, select CDN (LogFileCreated). For Event Name, enter Test_cdn_log_dump.

Use the parameter retrieved in the following steps to replace the
filePathparameter in the event template.Click Test Function. After the execution is complete, you can see that the return value is
nulland the status is successful.
In the OSS bucket console, select the bucket used to store CDN logs.
Select Objects and go to the directory configured to store CDN logs. You can see a folder named after the accelerated domain name. Inside this folder, there is a subfolder named after the date, which contains the file specified in the test. This indicates that the FC task has successfully processed the CDN offline log storage.


