All Products
Search
Document Center

Edge Security Acceleration:Getting started

Last Updated:Jan 08, 2025

This topic describes how to activate and use Edge Routine in the Edge Security Acceleration (ESA) console.

Prerequisites

  • A website has been added to ESA and is in the Active state. For information about how to add a website, see Add a website.

  • An SSL/TLS certificate is configured for the website before you access Edge Routine over HTTPS. For information about how to configure an SSL/TLS certificate, see Configure edge certificates.

Step 1: Create a routine

  1. Log on to the ESA console.

  2. In the left-side navigation pane, choose Edge Computing > Edge Routine.

  3. On the Edge Routine page, click Create Routine.

  4. In the Select Template step of the Create Routine page, select a template provided by ESA to generate routine code and click Next.

    image

  5. In the Configure Routine step, configure the parameters according to the following table and click OK.

    image

    Parameter

    Description

    Routine Name

    Enter a routine name.

    Note

    The name must be 2 to 41 characters in length and can contain lowercase letters, digits, and hyphens (-). The name must start with a lowercase letter and cannot end with a hyphen. Example: routine-name. After a routine is created, you cannot change the name of the routine.  

    Specification

    Select a specification. The specification specifies the maximum CPU time slice for a single execution of a routine. The I/O time to wait for network responses is not included in the CPU time slice. The maximum response time is 120 seconds. The available memory size is 128 MB. Fees vary based on routine specifications. For more information about billing, see Billing rules. Valid values:

    • CPU Time Slice for Individual Requests: 5ms (default)

    • CPU Time Slice for Individual Requests: 50ms

    • CPU Time Slice for Individual Requests: 100ms

    Description

    Optional. Enter a description for the routine.

    Code Preview

    Preview the routine code.

Step 2: Configure a trigger

Edge Routine of ESA allows you to use triggers to define the calling methods of scripts. You can associate a domain such as er.example.com with a routine. This way, all the traffic destined for the domain is directed to the routine. You can also add a route to specify matching URLs in a routine. In this case, a portion of the traffic destined for the specified URLs is directed to the routine. For example, if you add the er.example.com/test/path/ route in a routine, the traffic to er.example.com/test/path/ is directed to the routine.

  1. Log on to the ESA console.

  2. In the left-side navigation pane, choose Edge Computing > Edge Routine.

  3. On the Edge Routine page, find the routine that you created and click View Details in the Actions column.

    image

  4. On the routine details page, click the Triggers tab. You can associate a domain name or add a route to direct traffic destined for a specific domain to the routine.

    Associate a domain name

    You can connect your website to a routine by associating the domain name of your website with the routine. After you complete the association, you can directly use the domain name to access the routine.

    For example, if you use er.example.com to access a routine after you add the example.com website to ESA, all the requests destined for er.example.com, er.example.com/user, and er.example.com/login are forwarded to the routine.

    1. In the Custom Domains section, click Add Domain Name.

    2. Click OK.

      image

    Note

    If you connect your website to ESA in NS setup, ESA automatically adds a CNAME record for the domain name that you associated.

    If you connect your website to ESA in CNAME setup, ESA automatically assigns a CNAME for the domain name that you associated. You need to go to your DNS provider and add a CNAME record to map the domain name to the CNAME. For more information, see Add CNAME records to enable proxy.

    Add a route

    You can create a route to map request URLs to routines. If a request matches a specified URL that is mapped to a routine, the request goes to the routine. In other cases, ESA forwards the request to the origin server to pull the required content. In this section, the example.com website is used as an example. If you add the example.com/a* route for the example.com website in a routine, all the requests that match the route such as requests destined for example.com/a, example.com/a1, and example.com/a2 are processed by the routine. Requests that do not match the route such as requests destined for example.com/b, example.com/c, and example.com/d are forwarded to the origin server or points of presence (POPs).

    1. In the Route section, click Add Route.

    2. Select example.com from the Website drop-down list.

      image

    3. Enter a route in the Route field. You can add an asterisk (*) as the prefix or suffix of the route to match more URLs. For more information about how to match a route, see Matching rules.

      Note
      • Example 1: If you enter *.example.com/* in the Route field, requests destined for http://www.example.com/ or http://example.com/ are processed by the routine.

      • Example 2: If you enter example.com/a* in the Route field, requests destined for http://example.com/a, http://example.com/a1, or http://example.com/api are processed by the routine.

      • Example 3: If you enter www.example.com/api/* in the Route field, requests destined for http://www.example.com/api/ and requests destined for subpaths such as http://www.example.com/api/users and http://www.example.com/api/products/123 are processed by the routine.

    4. Click OK.

    Important

    If you enter a domain name with a prefix in the Route field such as *.example.com and www.example.com, you must manually add a record on the Records page in the ESA console to prevent access failures to the routine. For more information, see Add DNS records.

    Matching rules

    • When you configure a route, you must specify both the hostname of the domain name and the URI path. If you specify only the URI path, such as /path, the route cannot take effect.

    • When you configure a route, you can add an asterisk (*) as the prefix or suffix to match more URLs. An asterisk can match zero or more characters. For example, if you enter example.com/* in the Route field, requests destined for example.com are matched.

    • Routes are case-sensitive. For example, example.com/a and example.com/A specifies two different routes.

    • You cannot include an asterisk (*) or parameter within a route. For example, example.com/*/path and example.com/path?param=1 are considered invalid routes.

    • If a request matches multiple routes at the same time, the route that is configured at the earliest point in time is preferentially used.

Step 3: Develop and debug a routine

  1. Log on to the ESA console.

  2. In the left-side navigation pane, choose Edge Computing > Edge Routine.

  3. On the Edge Routine page, find the routine that you created and click View Details in the Actions column.

  4. image

  5. On the details page that appears, click the Code tab and develop the routine by using JavaScript (ES6 syntax).

    image

  6. Debug the routine code. ESA provides a debugging environment. After you write the code, you can click Save to Debugging Environment on the Code tab. Then, you can construct an HTTP request method, header, and body on the right side of the Code tab and click Request. ESA directly returns a response after the routine processes the request.

  7. After you debug the code, click Save as Version. On the Deployment tab, view the generated version.

    image

  8. Click Publish and select Test Environment. The code takes effect on the test POP. You can add a binding entry to your local hosts file. This allows you to initiate a request from a real client and view the response on the client.

    image

    Note

    We recommend that you deploy the version to all the canary environments before you deploy the version to the production environments.

Step 4: Release the official version

  1. On the details page that appears, click the Deployment tab. Click Release in the Actions column of the desired version.

  2. Select Production Environment. Click Release.

    image

  3. Click OK. Then, code of this version is deployed to all POPs of the production environments.