Configure an acceleration solution for Terraform init

更新时间:
复制 MD 格式

When terraform init times out due to network latency, providers may fail to download. Configure the Alibaba Cloud Terraform mirror to resolve this.

Symptoms

The Alibaba Cloud provider is available from two sources: source = hashicorp/alicloud and source = aliyun/alicloud. When running terraform init in China, you may see the following errors due to slow or failed connections to registry.terraform.io:

- Finding aliyun/alicloud versions matching "1.191.0"...
╷
│ Error: Failed to query available provider packages
│
│ Could not retrieve the list of available versions for provider aliyun/alicloud: could not query provider registry for registry.terraform.io/aliyun/alicloud: the request
│ failed after 2 attempts, please try again later: Get "https://registry.terraform.io/v1/providers/aliyun/alicloud/versions": net/http: request canceled (Client.Timeout
│ exceeded while awaiting headers)
╵

- Finding hashicorp/alicloud versions matching "1.191.0"...
╷
│ Error: Failed to query available provider packages
│
│ Could not retrieve the list of available versions for provider hashicorp/alicloud: could not query provider registry for registry.terraform.io/hashicorp/alicloud: the
│ request failed after 2 attempts, please try again later: Get "https://registry.terraform.io/v1/providers/hashicorp/alicloud/versions": context deadline exceeded
│ (Client.Timeout exceeded while awaiting headers)
╵

Solution

Terraform CLI v0.13.2 and later support network mirror configuration. The Alibaba Cloud Terraform mirror (https://mirrors.aliyun.com/terraform/) lets users in China download providers without connecting to registry.terraform.io.

  1. Create a Terraform CLI configuration file. The filename and location depend on your operating system.

  • Windows: Name the file terraform.rc and place it in your %APPDATA% folder. Run $env:APPDATA in PowerShell to find the exact path.

  • macOS and Linux: Name the file .terraformrc and place it in your home directory — /Users/username on macOS or /home/username on Linux. Run the following command to find your home directory:

    echo $HOME

    You can also use the TF_CLI_CONFIG_FILE environment variable to point to a Terraform CLI configuration file at any path. The filename must match the *.tfrc naming pattern.

    Add the following content to the file:

    provider_installation {
      network_mirror {
        url = "https://mirrors.aliyun.com/terraform/"
        # Restrict downloads to only Alibaba Cloud-related providers from the China mirror source.
        include = ["registry.terraform.io/aliyun/alicloud", 
                   "registry.terraform.io/hashicorp/alicloud",
                  ]   
      }
      direct {
        # All providers except Alibaba Cloud-related providers use their original download paths.
        exclude = ["registry.terraform.io/aliyun/alicloud", 
                   "registry.terraform.io/hashicorp/alicloud",
                  ]  
      }
    }
  1. Verify the configuration.

    In your Terraform template directory, run terraform init. The initialization is successful if the following output is displayed.

    b2af5798-dcdc-4abf-9823-7f3936ea7b0d.webp