All Products
Search
Document Center

Server Load Balancer:Add and manage scripts

Last Updated:Jul 24, 2024

Application Load Balancer (ALB) supports AScript, which allows you to configure scripts for listeners to define how requests are processed.

Prerequisites

A standard or WAF-enabled ALB instance is created. For more information, see Create an ALB instance.

Procedure

添加可编程脚本规则流程

Step 1: Create a listener for testing

You can create an HTTP, HTTPS, or QUIC listener for the ALB instance to test the scripts.

  1. Log on to the ALB console.
  2. In the top navigation bar, select the region where the ALB instance is deployed.

  3. Use one of the following methods to open the listener configuration wizard:

    • On the Instances page, find the ALB instance that you want to manage and click Operations in the Create Listener column.

    • On the Instances page, click the ID of the ALB instance that you want to manage. On the Listener tab, click Create Listener.

  4. In the Configure Listener step, configure the parameters that are described in the following table and click Next.

    Parameter

    Description

    Listener Protocol

    Select a listener protocol. In this example, HTTP is selected.

    Listener Port

    Select the port on which the ALB instance listens. The ALB instance listens for requests on the specified port and then forwards the requests to backend servers. Valid values: 1 to 65535. In most cases, port 80 is used for HTTP and port 443 is used for HTTPS.

    Note

    The ports on which an ALB instance listens must be unique.

    In this example, 80 is specified.

    Listener Name

    Enter a name for the listener.

    Advanced Settings

    You can click Modify to configure advanced settings. In this example, the default values are used for Advanced Settings.

  5. In the Server Group step, select a server type and a server group, and then click Next.

  6. In the Confirm step, confirm the configurations and click Submit.

Step 2: Create scripts to customize forwarding rules

We recommend that you stop the listener before you create a script. Otherwise, services may be affected.

  1. Log on to the ALB console.

  2. In the top navigation bar, select the region where the ALB instance is deployed.

  3. On the Instances page, click the ID of the ALB instance.

  4. On the Listener tab, click the ID of the listener that you created for testing.

  5. On the Listener Details tab, click the Forwarding Rules tab. On the Forwarding Rules tab, configure a script.

    1. Click Inbound Forwarding Rule or Outbound Forwarding Rule.

    2. Click Add Script Before Forwarding Rule Is Applied or Add Script After Forwarding Rule Is Applied.

      Note

      If Outbound Forwarding Rule is selected, you can click only Add Script Before Forwarding Rule Is Applied.

  6. On the Add Script page, configure the parameters and click OK.

    Parameter

    Required

    Description

    Rule Name

    Yes

    Enter a name for the script.

    Script Code

    Yes

    Specify the content of the script.

    • You can write a script or click Code Template to use a code template.

    • Write a script based on your business requirements. For more information, see AScript common scenarios.

    Run Script At

    Yes

    Specify the position where you want to execute the script. After the script is created, you cannot change the position where the script is executed.

    Status

    Yes

    Specify whether you want to enable the script.

    Advanced Settings

    No

    If a request carries the _es_dgb parameter and the value of the parameter is the specified key, the debug header is added to the response. The header carries the execution records of the script.

Step 3: Test the script-based forwarding rules on the listener

  1. Log on to the ALB console.

  2. In the top navigation bar, select the region where the ALB instance is deployed.

  3. On the Instances page, click the ID of the ALB instance.

  4. On the Listener tab, click the ID of the listener that you created for testing.

  5. In the upper-right corner of the Listener Details tab, click Start to enable the listener. If the listener is disabled, you must first enable the listener before you can apply the script-based forwarding rules.

  6. On the Forwarding Rules tab, enable the script-based forwarding rules.

    After you enable the listener, you can check whether network traffic can be distributed to backend servers based on the specified forwarding rules. If the result shows that network traffic can reach the backend servers, you can add the forwarding rules to a listener that serves the production environment.

Step 4: Apply the forwarding rules to the production environment

  1. Log on to the ALB console.

  2. In the top navigation bar, select the region where the ALB instance is deployed.

  3. Click the ID of the ALB instance.

  4. Click the Listeners tab and then click the ID of the listener that you want to manage.

  5. On the details page of the listener, click the Forwarding Rules tab. Add and enable the verified script-based forwarding rules on the Forwarding Rules tab. Then, the script-based forwarding rules are applied to the production environment.

What to do next: Manage the lifecycle of scripts

You can enable, disable, modify, and delete scripts as needed.

  • To enable a script, switch Disabled to Enabled for the script.

  • To disable a script, switch Enabled to Disabled for the script.

  • To modify a script, click Modify. You can modify the following information: Rule Name, Script Code, Status, and Advanced Settings.

  • To delete a script, click Delete. Then, click OK in the Delete Script message.

References