Configure AScript rules

Updated at:
Copy as MD

If standard forwarding rules do not meet your requirements, use AScript to create custom forwarding logic. This topic explains how AScript works and shows you how to configure its rules.

AScript overview

AScript is a programmable scripting feature in ALB. It includes built-in variables, conditional statements, and a function library for writing custom forwarding logic. Typical use cases include:

  • Custom authentication: Implement hotlink protection based on request parameters, cookies, or custom algorithms, or configure a User-Agent blocklist, Referer allowlist, and more.

  • Create IP-based blocklists and allowlists to control access.

  • Modify variables such as request parameters and request headers.

  • URL rewrite and redirect: Rewrite URIs and file extensions, or conditionally redirect requests to different addresses with a 302 status code.

For more information about AScript syntax and built-in variables, see AScript Syntax and Variable Reference. See AScript Use Cases for examples.

How it works

AScript rules, like standard forwarding rules, are used to process ALB requests. When a client request reaches an ALB listener, ALB processes it according to the configured forwarding rules. You can run AScript rules either before or after standard forwarding rules are processed.

AScript is designed to operate on a per-listener basis, allowing you to isolate different business logic into separate rules. You can run each rule at one of the following execution points:

  • Before/after inbound forwarding rule execution: Commonly used for scenarios like automatic file renaming, converting file extensions to lowercase, adding URI prefixes, and rewriting file extensions.

  • Before outbound forwarding rule execution: Commonly used for scenarios like automatic file renaming.

Prerequisites

  • You have created a Standard or WAF-enabled ALB instance. The Basic edition does not support AScript.

  • You have created a listener for the ALB instance.

Add an AScript rule

Test your AScript rule on a non-production listener first. After confirming that traffic is forwarded correctly, apply the rule to your production listener.

  1. Log on to the Instances page in the ALB console, and click the ID of the target instance.

  2. On the Listener tab, click the ID of the target listener. Click the Forwarding Rules tab. Click Inbound Forwarding Rules or Outbound Forwarding Rules, and then click Add Script Before Forwarding Rule Is Applied or Add Script After Forwarding Rule Is Applied.

    When you select Outbound Forwarding Rules, you can only select Add Script Before Forwarding Rule Is Applied.
  3. On the Add Script page, configure the following parameters and click OK.

    • Rule Name: Enter a custom rule name.

    • Script Code: Enter the script code, or click Code Template to select a scenario template. See AScript Use Cases for examples.

    • Run Script At: The point where the script runs. This value is determined by where you add the script and cannot be changed.

    • Status: Specifies whether the rule is enabled.

    • Advanced Settings (Optional): If a request includes the _es_dbg parameter and its value matches the key specified here, ALB enables debug response headers to output rule execution records. The debug response headers include the following fields:

      • Rule ID: The unique identifier for the rule, in the format as-****.

      • Execution status code and description:

        Execution status code

        Description

        Empty

        Not executed.

        0

        Executed and matched.

        Occurs when a rule contains an if condition {} block and the condition evaluates to true.

        1

        Executed but not matched.

        Occurs when a rule contains an if condition {} block and the condition evaluates to false, or when the rule does not contain an if condition {} block.

        2

        Execution exception.

      • Execution duration: The execution time in microseconds (us). The default value is -1.

      • Interrupted execution: Indicates whether the script's execution was interrupted. The default value is -1.

Manage AScript rules

On the Forwarding Rule tab, you can perform the following operations on AScript rules:

  • Click the switch next to the rule ID to enable or disable the rule.

  • Click Modify in the upper-right corner of the AScript card to modify the rule configuration.

  • Click Delete in the upper-right corner of the AScript card, and then click OK in the dialog box.

Billing

AScript rules contribute to the number of rule evaluations, a metric used for Loadbalancer Capacity Unit (LCU) billing. For more information, see ALB Billing.