如果在执行 Terraform init 时出现 “Registry service unreachable.” 的错误,应该采取以下解决方法

环境

Terraform 0.12.9
苹果操作系统 Mojave 10.14.6

错误

当执行 terraform init 时,显示了以下错误。

$ terraform init

Initializing the backend...

Initializing provider plugins...
- Checking for available provider plugins...

Registry service unreachable.

This may indicate a network issue, or an issue with the requested Terraform Registry.


Error: registry service is unreachable, check https://status.hashicorp.com/ for status updates

怎样才能解决这个问题呢? ne?)

我认为更改版本可能会解决问题,所以我使用了tfenv将版本从0.12.9更改为0.11.14,并再次运行了terraform init命令,结果出现了以下错误…

Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...

Error installing provider "aws": Get https://releases.hashicorp.com/terraform-provider-aws/: dial tcp: i/o timeout.

Terraform analyses the configuration and state and automatically downloads
plugins for the providers used. However, when attempting to download this
plugin an unexpected error occured.

This may be caused if for some reason Terraform is unable to reach the
plugin repository. The repository may be unreachable if access is blocked
by a firewall.

If automatic installation is not possible or desirable in your environment,
you may alternatively manually install plugins by downloading a suitable
distribution package and placing the plugin's executable file in the
following directory:
    terraform.d/plugins/darwin_amd64

根据最后一句话的意思,似乎需要手动下载插件。

进一步查看这个网站(Terraform Provider がないエラー対処 | infraya.work),似乎可以从https://releases.hashicorp.com/下载插件。

解决方案

因此,从https://releases.hashicorp.com/下载并解压适用的插件zip文件,将其中的可执行文件放置在/Users/用户名/.terraform.d/plugins/文件夹下,问题得以解决。

$ terraform init

Initializing the backend...

Initializing provider plugins...

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

补充说明

因为我的情况是缺少AWS插件,所以我下载了它。