Connect Function Compute and CDN with the CDN Trigger and Fun

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 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.
Image for post
Image for post

Tutorial Procedure:

Image for post
Image for post

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.

2. Describe the CDN Trigger

You can use Fun to develop, build, and deploy resources by describing specified resources in the file. You need to describe the CDN trigger in the file by following the specifications:

Below is the description of the Trigger parameters:

  • is the name of an event that triggers function execution on the CDN and cannot be changed once created.
  • is the version of an event that triggers function execution on the CDN and cannot be changed once created.
  • is the note information about the trigger.
  • specifies filters (at least one filter is required).
  • is the domain name managed in the CDN. It is of type Array.

In the preceding example, we have defined a CDN trigger called . 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.

Image for post
Image for post

3. Deploy the trigger

Deploy the trigger by using :

Image for post
Image for post

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

Image for post
Image for post

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 , , and :

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

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

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