By Sun Feiyu
In this tutorial, you will learn how to use Function Compute and Fun to configure and deploy the CDN trigger. But before we dive into anything in this tutorial, let's first take a look at several of the important concepts mentioned in this article and also discuss some things you'll need to complete before starting this tutorial.
First, there's Function Compute, which is an event-driven service that allows users to write and upload code without needing to manage server health along with other factors. Function Compute prepares and auto-scales to the correct amount of computing resources to run user code. The user only pays for the resources required to run their code.
Next, there's Fun, which is a tool that supports serverless application deployment and allows easy management of resources such as Function Compute, API Gateway, and Log Service. You can use Fun to develop, build, and deploy resources by describing specified resources in the template.yml
file.
Last, there's also the concept of a Content Delivery Network. In this article, we will be specifically concerned with the Alibaba Cloud Content Delivery Network (CDN), which is Alibaba Cloud's version of this service - which most cloud provider provide nowadays. This service can be understood as providing a distributed network that is built on and overlays with the bearer network, and which also is composed of edge node server clusters distributed across different regions. CDN replaces the traditional data transmission model based on Web Servers, and rather, specifically in our case, it stores resources on the source stations to Alibaba Cloud edge servers closer to your specific region to allow users quickly obtain resources nearby. This improves the user experience and reduces the burden on the source stations.
Before you begin this tutorial, make sure that you have completed the following:
For this tutorial, you will be using Function Compute and CDN together, which can be seamlessly integrated with each other. More specifically, you will learn how Fun uses the CDN trigger to connect CDNs with Function Compute. To do all of this, follow these steps below:
To use the CDN trigger, you need to adjust your function code as shown below, with write handlers following the CDN event format.
exports.handler = function(event, context, callback) {
var eventObj = JSON.parse(event.toString());
console.log("event: " + event);
console.log('context: ', JSON.stringify(context));
console.log('eventName: '+ eventObj.events[0].eventName);
console.log('eventVersion: '+ eventObj.events[0].eventVersion);
console.log('eventSource: '+ eventObj.events[0].eventSource);
console.log('region: '+ eventObj.events[0].region);
console.log('eventParameter: '+ JSON.stringify(eventObj.events[0].eventParameter,null,4))
// cdn trigger example: https://help.aliyun.com/document_detail/73333.html
callback(null, 'hello cdn_trigger');
};
You can use Fun to develop, build, and deploy resources by describing specified resources in the template.yml
file. You need to describe the CDN trigger in the yml
file by following the specifications:
Events:
cdn-trigger-name: # trigger name
Type: CDN # trigger type
Properties:
EventName: CachedObjectsRefreshed
EventVersion: '1.0.0'
Notes: cdn events trigger test
Filter:
Domain: ['cdn-trigger.sunfeiyu.top']
Below is the description of the Trigger parameters:
EventName
is the name of an event that triggers function execution on the CDN and cannot be changed once created.EventVersion
is the version of an event that triggers function execution on the CDN and cannot be changed once created.Notes
is the note information about the trigger.Filter
specifies filters (at least one filter is required).Domain
is the domain name managed in the CDN. It is of type Array.In the preceding example, we have defined a CDN trigger called cdn-trigger-name
. CachedObjectsRefreshed
is a resource refresh event, and once it is triggered under the domain name, it will force CDN nodes to go back to the origin and pull the latest files.
The following table provides events and versions currently supported by a CDN event trigger.
Deploy the trigger by using fun deploy
:
You can view the CDN trigger that you have configured in the Function Compute console:
After the CDN system detects a relevant event, it encodes the event information as a JSON string and transmits it to the corresponding event processing function. Below are some example event format for CachedObjectsRefreshed
, CachedObjectsPushed
, and CachedObjectsBlocked
:
{
"events": [
{
"eventName": "CachedObjectsRefreshed", // Event type
"eventVersion": "1.0.0", //Event version, currently it's version 1.0.0
"eventSource": "cdn", // The Event source name
"region": "cn-shanghai", // The region. The default is "cn-hangzhou"
"eventTime": "2018-03-16T14:19:55+08:00", // The time the event occurred
"traceId": "cf89e5a8-7d59-4bb5-a33e-4c3d08e25acf", // The ID sent for the event source, used for queries.
"resource": {
"domain": "cdn-trigger.sunfeiyu.top" // The domain of the resource
},
"eventParameter": {
"objectPath": [
"/2018/03/16/13/33b430c57e7.mp4", // The object path
"/2018/03/16/14/4ff6b9bd54d.mp4"
],
"createTime": 1521180769,
"domain": "cdn-trigger.sunfeiyu.top",
"completeTime": 1521180777,
"objectType": "File", // To change types, the value is File, Directory
"taskId": 2089687230 // The resource task ID
},
"userIdentity": {
"aliUid": "1xxxxxxxxxx" // the account ID for the main account }
}
]
}
Collapse the event and you will have a more intuitive view:
{
"events":Array[1]
}
Below are some points that are relevant to the things we learned in this blog:
Develop Function Compute by Installing Third-Party Dependencies
99 posts | 7 followers
FollowAlibaba Clouder - March 18, 2021
Alibaba Clouder - July 29, 2020
Alibaba Cloud Serverless - May 9, 2020
JDP - July 17, 2020
Alibaba Cloud Native Community - August 25, 2022
Alibaba Clouder - June 16, 2020
99 posts | 7 followers
FollowAlibaba Cloud Function Compute is a fully-managed event-driven compute service. It allows you to focus on writing and uploading code without the need to manage infrastructure such as servers.
Learn MoreAlibaba Cloud offers an accelerated global networking solution that makes distance learning just the same as in-class teaching.
Learn MoreConnect your business globally with our stable network anytime anywhere.
Learn MoreVisualization, O&M-free orchestration, and Coordination of Stateful Application Scenarios
Learn MoreMore Posts by Alibaba Cloud Serverless