This topic describes how to enable the EdgeRoutine feature and get started with EdgeRoutine.

Workflow

Before you can use the EdgeRoutine feature, you must activate the Dynamic Route for CDN (DCDN) service. In this topic, HelloWorld is used as an example to show you how to get started with EdgeRoutine. 开通边缘程序

Enable EdgeRoutine

  1. Log on to the DCDN console.
  2. In the left-side navigation pane, choose EdgeRoutine.
  3. On the EdgeRoutine page, choose Activate EdgeRoutine > Activate Now.

Create and configure a routine

  1. Log on to the DCDN console.
  2. In the left-side navigation pane, choose EdgeRoutine.
  3. On the EdgeRoutine page, click Create Subdomain and enter a subdomain name.
    Note
    • A subdomain is a repository in which all routines that you create reside. After a subdomain is created, you cannot modify the name of the subdomain.
    • A subdomain is a unique identifier for each user and a part of the routine ID.
    • The subdomain name can contain lowercase letters, digits, and hyphens (-).
  4. On the EdgeRoutine page, click Create Routine.
    创建程序
    Parameter Description
    Routine Name Enter a routine name.
    Note The name must be at least 2 characters in length, and can contain lowercase letters, digits, and hyphens (-). Example: routine-name. After a routine is created, you cannot modify the name of the routine.
    Description Optional. The description of the routine.
    Specification The maximum CPU time slice for a single execution of a routine. The I/O time to wait for network responses is not included. The fees vary based on routine specifications. Default value: 50 ms. For more information about billing, see Billing.
    • CPU Time Slice for Individual Requests: 5 ms. Maximum Real Time: 120s. Memory: 128 MB.
    • CPU Time Slice for Individual Requests: 50 ms. Maximum Real Time: 120s. Memory: 128 MB.
    • CPU Time Slice for Individual Requests: 100 ms. Maximum Real Time: 120s. Memory: 128 MB.
  5. After a routine is created, you can click Details to configure the settings of the routine in different environments. Routine settings in each environment are independent. You can modify the routine settings in the production environment, test environment, and Canary release environment (optional).
    详情
    Parameter Description
    Routine Specification The maximum CPU time slice for a single execution of a routine. The I/O time to wait for network responses is not included. The fees vary based on routine specifications. Default value: 50 ms.
    • CPU Time Slice for Individual Requests: 5 ms. Maximum Real Time: 120s. Memory: 128 MB.
    • CPU Time Slice for Individual Requests: 50 ms. Maximum Real Time: 120s. Memory: 128 MB.
    • CPU Time Slice for Individual Requests: 100 ms. Maximum Real Time: 120s. Memory: 128 MB.
    Domain Name Whitelist
    • The list of domain names that can be associated with the routine.
    • Wildcard domain names are supported.
    • By default, the Domain Name Whitelist is empty. Requests from all domain names can be associated with the routine.
    Note For example, if you set Domain Name Whitelist to example.com, only requests from example.com can trigger the routine. Requests from other domain names are rejected by the routine.

Associate domain names with a routine

After a routine is created and configured, you need to associate domain names of Alibaba Cloud CDN or DCDN with the routine. The routine is triggered by requests from the domain name that is associated with the routine.

  1. Log on to the Alibaba Cloud CDN console or DCDN console.
    Note A routine requires a domain name to provide services for clients. The domain name can be a CDN accelerated domain name or a DCDN accelerated domain name. Log on to the console of the service in which your domain name resides.
  2. In the left-side navigation pane, click Domain Names.
  3. On the Domain Names page, find the domain name that you want to manage and click Manage or Configure.
  4. In the left-side navigation pane of the specified domain name, click EdgeRoutine.
  5. On the EdgeRoutine page, enable and configure a Routine ID.
    边缘程序
    Parameter Description
    Routine ID The routine ID is a unique identifier for each routine. Format: routine name.subdomain.
    Note In the left-side navigation pane of the DCDN console, choose Routines, select the routine whose ID you want to view, and click Details. In the Basic Information section on the Settings tab, you can view the routine ID.
    Trigger Mode Requests to access the domain name of Alibaba Cloud CDN or DCDN trigger the routine. The following trigger modes are supported:
    • Redirect: Requests to access the domain name of Alibaba Cloud CDN or DCDN are processed by the routine. You can customize the code in the routine to process requests. The routine can directly return the requested content to the client, read the cache by calling the Cache API, or redirect requests to the origin server by using the fetch request.
    • Bypass: Requests to access the domain name of Alibaba Cloud CDN or DCDN trigger the routine. The routine processes the requests, settings that are configured in the Alibaba Cloud CDN or DCDN console are executed, and then requests are redirected to the origin server. This trigger mode is suitable for scenarios such as authentication or log tracking.
    Redirect to Origin if Error Occurs
    • Yes: When an exception occurs in your routine, the edge node forwards the request to your origin server by using the URL that is requested by the client. The origin server processes the request. This ensures business continuity and disaster recovery.
    • No: When an exception occurs in your routine, a 5xx status code is returned to the client.
      Note We recommend that you set the Redirect to Origin if Error Occurs parameter to No when you first use a routine. You can use the returned 5xx status code to troubleshoot issues.

Develop and test a routine

  1. Log on to the DCDN console.
  2. In the left-side navigation pane, choose EdgeRoutine.
  3. Select a routine that you created and click Details.
  4. On the Details page, click Code to develop a routine by using JavaScript. In the sample code, HelloWorld is used.代码
  5. After you develop the code, click Save and Publish to Test Environment. 测试You can choose an IP address of the test environment that is provided on the page to bind your on-premises machine and test the code. image02
    Note To bind your on-premises machine, you can add the IP address of the test environment and the domain name that is associated with the routine to the host file of your on-premises machine. For example, add 10.10.10.10 example.com to the host file of your on-premises machine. Replace the IP address with an IP address that is provided in the preceding figure, and replace the domain name with the domain name of Alibaba Cloud CDN or DCDN that is associated with the routine.

Save a version

After the code is tested, click Save as Version on the Code page.

Publish a version

On the Details page, click Versions, select a version that you want to publish, and click Publish. Select the production environment to which you want to publish the version. The code of this version is deployed on all nodes in the production environment.

Iteration

The preceding sections use HelloWord to describe how to configure and publish a routine.
  • For the iteration of the code, see the "Develop and test a routine", "Save as a version", and "Publish a version" sections.
  • If you need to adjust configurations such as the routine specifications or domain name whitelist, see the "Create and configure a routine" section.
  • If you want to disable the EdgeRoutine feature for Alibaba Cloud CDN or DCDN domain names, delete the routine configurations that are configured in the "Associate domain names with a routine" section.