文档

Terraform Init 加速方案配置

更新时间:

当您遇到由于网络延迟等原因造成的 terraform init 超时,导致无法正常下载 Provider 等情况时,可通过配置阿里云镜像站解决。

问题描述

阿里云 Provider 提供两个下载源:source = hashicorp/alicloudsource = aliyun/alicloud。国内用户在下载 Provider 时可能会遇到下载缓慢甚至下载失败的问题:

- 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)
╵

解决方案

Terraform CLI 自 0.13.2 版本起提供了设置网络镜像的功能。为解决以上问题,阿里云 Provider 提供了镜像服务以帮助国内用户快速下载。

配置方案

创建.terraformrcterraform.rc配置文件,文件位置取决于主机的操作系统。

  • 在 Windows 环境上,文件必须命名为terraform.rc,并放置在相关用户的%APPDATA%目录中。这个目录的物理位置取决于Windows 版本和系统配置;在 PowerShell 中使用 $env:APPDATA 可以找到其在系统上的位置。

  • 在所有其他系统上,必须将该文件命名为.terraformrc,并直接放在相关用户的主目录中。

也可以使用TF_CLI_CONFIG_FILE环境变量指定 Terraform CLI 配置文件的位置,任何此类文件都应遵循命名模式*.tfrc

以 macOS 为例,在home目录下创建.terraformrc文件,内容如下:

provider_installation {
  network_mirror {
    url = "https://mirrors.aliyun.com/terraform/"
    // 限制只有阿里云相关 Provider 从国内镜像源下载
    include = ["registry.terraform.io/aliyun/alicloud", 
               "registry.terraform.io/hashicorp/alicloud",
              ]   
  }
  direct {
    // 声明除了阿里云相关Provider, 其它Provider保持原有的下载链路
    exclude = ["registry.terraform.io/aliyun/alicloud", 
               "registry.terraform.io/hashicorp/alicloud",
              ]  
  }
}

  • 本页导读 (0)
文档反馈