All Products
Search
Document Center

Edge Security Acceleration:Configure redirect rules using Terraform

Last Updated:Mar 28, 2026

You can use Terraform to quickly configure redirect rules for a site. This allows you to redirect matching requests to a target URL with a specific status code.

Install Terraform and configure permissions

Install and configure Terraform on your computer

For more information about how to use Terraform on your computer, see Install and configure Terraform.

  • Create an AccessKey pair for a RAM user. An Alibaba Cloud account has all permissions on resources. If the AccessKey pair of your Alibaba Cloud account is leaked, your resources are exposed to great risks. We recommend that you use the AccessKey pair of a RAM user. For more information, see Create an AccessKey pair.

  • Create environment variables to store identity credentials.

    You can create and view your AccessKey on the AccessKey Management page.

    If the environment variables are not configured, identity verification fails when you run the Terraform template.

Use an online service (no installation or permission configuration required)

If you do not want to install Terraform, you can use the online service Cloud Shell.

Alibaba Cloud Cloud Shell is a free operations and maintenance (O&M) product. It is pre-installed with Terraform components and configured with identity credentials. Therefore, you can run Terraform commands directly in Cloud Shell. For more information, see Create resources with Terraform.

Important

When you use Terraform in Cloud Shell, its destroy feature can cause data loss. We recommend that you use Cloud Shell only for simple and quick operations, such as debugging. For more information about the limits, see Limits.

Resources

Configure a redirect rule

  1. Create a configuration file named main.tf and copy the following code into it. This example creates a redirect rule that redirects all requests to a fixed URL with a 302 status code and discards the original query parameters.

    # 1. Enter the name of the site to configure.
    data "alicloud_esa_sites" "default" {
      site_name = "{{DOMAIN}}"  # Replace DOMAIN with the actual site connected to ESA, such as example.com.
    }
    
    # 2. Configure the redirect rule: Redirect all requests with a 302 status code to http://test.example.com/image/1.jpg?test=123 and ignore the original query parameters.
    resource "alicloud_esa_redirect_rule" "default" {
      site_id               = data.alicloud_esa_sites.default.sites[0].site_id  # The ID of the site to configure.
      rule_name             = "example2"  # The name of the rule.
      rule_enable           = "on"  # Enable the rule.
      rule                  = "true"  # The match condition. A value of "true" matches all incoming requests.
      type                  = "static"
      target_url            = "http://test.example.com/image/1.jpg?test=123"  # The target URL for the redirect.
      status_code           = "302"  # The redirect status code.
      reserve_query_string  = "off"  # Specifies whether to include the original query string in the target URL.
    }
    
  2. Navigate to the directory that contains the configuration files. Then, run the following command to initialize the Terraform environment.

    terraform init

    image

  3. Run the following command to validate the syntax and configuration of the Terraform files.

    terraform validate

    If the output is similar to the following figure, the validation is successful.

    image

  4. Run the following command to preview the changes that will be applied.

    terraform plan
  5. Run the following command to execute the Terraform script.

    terraform apply
  6. When prompted, enter yes to confirm the operation.

Verify results

Terraform show

In your working directory, run the following command to view detailed information about the resources created by Terraform:

terraform show

Console

  1. On the ESA console, select Site Management. In the Website column, click the target site.

  2. In the navigation pane on the left, choose Rules > Redirect Rules. The page lists the redirect rule you created and its configuration.

(Optional) Clean up resources

If you no longer need the resources created or managed by Terraform, you can run the terraform destroy command to release the resources.

terraform destroy

References

Type parameter

The following table describes the valid values for the type parameter in the alicloud_esa_redirect_rule resource.

Value

Description

static

Static. The target URL is a fixed value.

dynamic

Dynamic. The target URL is dynamically generated by using an expression.

Status_code parameter

The following table describes the common values for the status_code parameter in the same resource.

Value

Description

301

Permanent redirect (301 Moved Permanently)

302

Temporary redirect (302 Found)

303

Temporary redirect (303 See Other)

307

Temporary redirect (307 Temporary Redirect)

308

Permanent redirect (308 Permanent Redirect)