更新 Terraform tfstate 文件的提供者

首先

我尝试更新由Terraform v0.12.29创建的资源时,遇到了无法更新远程状态文件的提供程序的问题,我经历了一番困扰并找到了解决方法。

执行环境

    • macOS Big Sur 11.4

 

    Terraform v0.14.3

经过

大约半年前我有机会更新使用Terraform创建的AWS资源,然后当我修改tf文件后运行了terraform plan。

Error: Could not load plugin


Plugin reinitialization required. Please run "terraform init".

Plugins are external binaries that Terraform uses to access and manipulate
resources. The configuration provided requires plugins which can't be located,
don't satisfy the version constraints, or are otherwise incompatible.

Terraform automatically discovers provider requirements from your
configuration, including providers used in child modules. To see the
requirements and constraints, run "terraform providers".

插件加载失败,导致出现错误。

由于被要求重新执行 terraform init,因此我进行了执行。

Error: Invalid legacy provider address

This configuration or its associated state refers to the unqualified provider
"archive".

You must complete the Terraform 0.13 upgrade process before upgrading to later
versions.


Error: Invalid legacy provider address

This configuration or its associated state refers to the unqualified provider
"aws".

You must complete the Terraform 0.13 upgrade process before upgrading to later
versions.

尽管被要求升级到Terraform 0.13,但实际执行版本却是0.14.3,再次出现错误(provider使用了AWS,用于Lambda的zip存档)。

由于state文件是在S3中进行管理的,因此我尝试删除.terraform目录并重新运行terraform init,但结果并没有改变。

于是我检查了存储在S3上的tfstate文件,结果发现”terraform_version”为”0.12.29″。
嗯,看起来远程state文件没有更新成功。

解决方案 (Jiě jué àn)

看起来从v0.13开始,提供者的寄件人地址指定方法似乎已经改变了,
因此我用以下命令更新了state文件中的提供者。

terraform state replace-provider registry.terraform.io/-/aws hashicorp/aws
terraform state replace-provider registry.terraform.io/-/archive hashicorp/archive

在此之后,我成功地通过terraform init更新了terraform版本,
并且也能够执行terraform apply!

最终

看起来从v0.12.29升级到v0.13时需要遵循一系列步骤,这些步骤在官方的v0.13升级指南中有详细说明呢…

顺便说一下,在升级到v0.13后,有一些资源可能会出现差异,尽管没有更改tf文件,所以请注意是否存在意外改变。

广告
将在 10 秒后关闭
bannerAds