AScript syntax and variable reference

Updated at:
Copy as MD

AScript is a programmable script for Application Load Balancer (ALB) that lets you create custom forwarding rules.

Syntax rules

Important

Double quotation marks (") are not allowed in AScript. You must use single quotation marks (') for all string values.

Syntax

Rule

Comment

Any text on a line that follows a pound sign (#) is considered a comment. For example: # this is annotation.

Identifier

  • An identifier can contain letters, digits, and underscores (_), but cannot start with a digit. Identifiers are case-sensitive.

  • This rule applies to the names of both built-in and custom variables and functions.

Data type

  • String

    Literal constant: A value enclosed in single quotes, for example: 'hello, AScript'.

  • Number

    Literal constant: A decimal number. For example: 10, -99, or 1.1.

  • Boolean

    Literal constant: true or false.

  • Dictionary

    The following are examples of literal constants:

    • []: An empty dictionary.

    • ['key1', 'key2', 100]:

      • 1 -> 'key1'

      • 2 -> 'key2'

      • 3 -> 100

    • ['key1' = 'value1', 'key2' = 1000]

      • 'key1' -> 'value1'

      • 'key2' -> 1000

Variable

  • Definition

    A variable is defined when a value is assigned to it.

  • Usage

    • Reference built-in and custom variables by their names.

      • Referencing a built-in variable: host.

      • Referencing a custom variable: seckey.

    • You can use the $ symbol to reference the built-in properties of a variable.

      Referencing a built-in variable: $host.

    • A custom variable cannot have the same name as a built-in variable.

      For a list of built-in variables, see the Built-in variables table below.

Operator

  • = : The assignment operator.

    • For example: seckey = 'ASDLFJ234dxvf34sDF'

    • For example: seckeys = ['key1', 'key2']

  • - : The unary minus operator.

    For example: inum = -10

  • AScript does not support other operators. Instead, operations on data types are handled by built-in functions. For more information, see Overview of AScript built-in functions.

Statement

  • Conditional statement

    if condition {   
       ...
    }
    
    if condition1 {   
       if condition2 {
            ...
       }
    }
    
    if condition {
       ...
    } else {
       ...
    }
  • Explanation

    • A condition can consist of the following syntax elements:

      • Literal value

      • Variable

      • Function call

    • Body

      • The body can be empty.

      • Multiple statements are allowed. Write one statement per line.

    • Multi-level nesting is supported.

    • Coding Style

      The syntax requires the opening curly brace to follow the if condition on the same line.

Function

  • Definition syntax

    def function_name(parameter_list) {
        ...
    }
  • Definition details

    • Parameter list

      • Functions can be defined without parameters.

      • Separate multiple parameters with commas.

    • Function body

      • The body can be empty.

      • Multiple statements are allowed. Write one statement per line.

      • Return value: The return statement is supported.

    • Coding Style

      The syntax requires the opening curly brace to follow the def function name (parameter list) and be on the same line.

  • Function call

    Both built-in and custom functions are called by using function_name(parameter_list).

Built-in variables

  • The dollar sign ($) before a built-in variable name is used only to identify it as a built-in variable and can be omitted without affecting its use.

  • Built-in variables are read-only and cannot be used as an l-value. This means you cannot assign a value to a built-in variable.

  • Each AScript rule supports a maximum of 200 global variables. There is no limit on the number of local variables. To use more than 200 variables, define a custom function and use local variables within that function.

Variable name

Description

Equivalent Nginx variable

$arg_{name}

The value of the name parameter in the Query String. The Query String contains the request parameters of an HTTP request.

ngx.var.arg_{name}

The hyphens (-) in {name} must be replaced with underscores (_). For example, X-USER-ID corresponds to $arg_x_user_id.

$http_{name}

The value of name in the request header.

ngx.var.http_{name}

Hyphens (-) in {name} must be replaced with underscores (_). For example, X-USER-ID becomes $http_x_user_id.

$cookie_{name}

The value of name in the request cookie header.

ngx.var.cookie_{name}

Hyphens (-) in {name} must be replaced with underscores (_). For example, X-USER-ID becomes $cookie_x_user_id.

$scheme

The protocol.

ngx.var.scheme

$server_protocol

The protocol version.

ngx.var.server_protocol

$host

The host name from the request.

ngx.var.host

$uri

The raw request URI.

ngx.var.raw_uri

$args

$args represents the query string of the current HTTP request, but does not include the question mark (?). For example: http://www.a.com/1k.file?k1=v1&k2=v2.

  • You can use $arg_k1 to obtain the corresponding v1 value.

  • The $args variable contains the entire query string, such as k1=v1&k2=v2, and does not include the question mark (?).

ngx.var.args

$request_method

The request method.

ngx.var.request_method

$request_uri

The content of uri+'?'+args.

ngx.var.request_uri

$remote_addr

The client IP address.

ngx.var.remote_addr