Connect Function Compute and CDN with the CDN Trigger and Fun

Background Information

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.

Before You Begin

Before you begin this tutorial, make sure that you have completed the following:

  • Activate Alibaba Cloud Function Compute and Alibaba Cloud Content Delivery Network.
  • Understand the associated terms, including things like source stations and canonical domain names.
  • Add a CDN accelerating domain, and then go to the DNS server provider to add the CNAME record.

Tutorial Procedure:

1. Write Functions

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');
};

2. Describe the 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']
  • 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.

3. Deploy the trigger

Deploy the trigger by using fun deploy:

Understanding the CDN Event Format

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 }
}
]
}
{
"events":Array[1]
}

Summary

Below are some points that are relevant to the things we learned in this blog:

  • An accelerating domain cannot be used as the source station of a CDN. Otherwise, cyclical resolution would occur, making back-to-origin impossible.
  • Once an EventName or EventVersion is created for a CDN trigger defined in a Function (a unit), they cannot be changed.
  • Warmup is suitable for publishing resources to the CDN for the first time and refreshing is suitable for actively updating files in the CDN to the latest version after files are modified.
  • URL refreshing and folder refreshing are supported. After files are hit, CDN will record files under each folder to obtain the URL of each file. Folder refreshing is essentially the “packaging” of URL refreshing.
  • Alibaba Cloud CDN features an advanced distributed system architecture and has 2500+ global nodes that cover six continents. Alibaba Cloud CDN shows excellent and stable performance: 95%+ hit rate, response time in milliseconds, 95%+ video smoothness, and the largest number of nodes in China.

Original Source

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com