Community Blog Connect Function Compute and CDN with the CDN Trigger and Fun

Connect Function Compute and CDN with the CDN Trigger and Fun

In this tutorial, you will learn how the tool Fun uses the CDN trigger to connect CDNs with Function Compute.

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.

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.

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

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:


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:

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:

  cdn-trigger-name: # trigger name
    Type: CDN # trigger type
      EventName: CachedObjectsRefreshed
      EventVersion: '1.0.0'
      Notes: cdn events trigger test
        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.


3. Deploy the trigger

Deploy the trigger by using fun deploy:


You can view the CDN trigger that you have configured in the Function Compute console:


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
        "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:



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 2800+ 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.
0 0 0
Share on

Alibaba Cloud Serverless

97 posts | 7 followers

You may also like


Alibaba Cloud Serverless

97 posts | 7 followers

Related Products