Configure an acceleration solution for Terraform init
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.
Create a Terraform CLI configuration file. The filename and location depend on your operating system.
Windows: Name the file
terraform.rcand place it in your%APPDATA%folder. Run$env:APPDATAin PowerShell to find the exact path.
-
macOS and Linux: Name the file
.terraformrcand place it in your home directory —/Users/usernameon macOS or/home/usernameon Linux. Run the following command to find your home directory:echo $HOMEYou can also use the
TF_CLI_CONFIG_FILEenvironment variable to point to a Terraform CLI configuration file at any path. The filename must match the*.tfrcnaming 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", ] } }
-
Verify the configuration.
In your Terraform template directory, run
terraform init. The initialization is successful if the following output is displayed.