All Products
Search
Document Center

:Configure an acceleration solution for Terraform initialization

Last Updated:Mar 18, 2025

If the terraform init command times out due to reasons such as network latency, issues such as the inability to download providers may occur. To resolve the issues, you can specify the Alibaba Cloud open source image website.

Issue

You can download the Alibaba Cloud provider from the following sources: source = hashicorp/alicloud and source = aliyun/alicloud. You may encounter slow download speeds or download failures when you download the provider in China:

- 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 versions allow you to configure network images. To solve the preceding issues, the Alibaba Cloud provider provides an image service to help users in China download images in a fast manner.

Configuration method

Create a configuration file named .terraformrc or terraform.rc. The file location varies based on the operating system of your host.

  • On a Windows operating system, the file must be named terraform.rc and stored in the %APPDATA% directory of the user. The specific directory varies based on the Windows version and system configurations. You can run the $env:APPDATA command in PowerShell to find the file directory in your operating system.

  • On all other operating systems, the file must be named .terraformrc and stored in the home directory of the related user.

You can also use the TF_CLI_CONFIG_FILE environment variable to specify the directory of the configuration file for the Terraform CLI. The configuration file must follow the *.tfrc naming format.

If you use macOS, create a file named .terraformrc in the home directory. The file contains the following content:

provider_installation {
  network_mirror {
    url = "https://mirrors.aliyun.com/terraform/"
    // Only Alibaba Cloud-related providers can be downloaded from image sources in the Chinese mainland.
    include = ["registry.terraform.io/aliyun/alicloud", 
               "registry.terraform.io/hashicorp/alicloud",
              ]   
  }
  direct {
    // All providers except Alibaba Cloud-related providers maintain the original download links.
    exclude = ["registry.terraform.io/aliyun/alicloud", 
               "registry.terraform.io/hashicorp/alicloud",
              ]  
  }
}