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.
- Log on to the ALB console.
In the top navigation bar, select the region where the ALB instance is deployed.
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.
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.
NoteThe 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.
In the Server Group step, select a server type and a server group, and then click Next.
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.
Log on to the ALB console.
In the top navigation bar, select the region where the ALB instance is deployed.
On the Instances page, click the ID of the ALB instance.
On the Listener tab, click the ID of the listener that you created for testing.
On the Listener Details tab, click the Forwarding Rules tab. On the Forwarding Rules tab, configure a script.
Click Inbound Forwarding Rule or Outbound Forwarding Rule.
Click Add Script Before Forwarding Rule Is Applied or Add Script After Forwarding Rule Is Applied.
NoteIf Outbound Forwarding Rule is selected, you can click only Add Script Before Forwarding Rule Is Applied.
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
Log on to the ALB console.
In the top navigation bar, select the region where the ALB instance is deployed.
On the Instances page, click the ID of the ALB instance.
On the Listener tab, click the ID of the listener that you created for testing.
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.
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
Log on to the ALB console.
In the top navigation bar, select the region where the ALB instance is deployed.
Click the ID of the ALB instance.
Click the Listeners tab and then click the ID of the listener that you want to manage.
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
For more information about AScript, see AScript overview.
For more information about how AScript works, see How AScript works.
For more information about how to configure ALB listeners, see the following topics: