Terraform 升级提供者

Terraform Upgrade Providers

在现有的 Terraform 目录中:

~ terraform version  
Terraform v0.11.11
+ provider.aws v1.51.0

如果我设置一个新的 Terraform 目录:

~ terraform version
Terraform v0.11.11
+ provider.aws v1.55.0

如何升级我的 provider.aws?如果我在 .tf 文件的 provider "aws" 中设置 version = "~> 1.55.0",我会得到一个错误:

* provider.aws: no suitable version installed
  version requirements: "~> 1.55.0"
  versions installed: "1.51.0"

我希望找到 terraform update 命令或类似的东西。但是我找不到。

我不应该升级提供商吗?我是否需要删除状态,重新运行 init 然后 refresh?或者有更好的方法吗?

只需运行terraform init升级AWS插件版本,无需删除状态文件。

解决这个问题有两种方案:

  1. 只需删除 terraform 缓存 rm -fr .terraform 并再次执行 terraform init。如果 Terraform 状态在该文件夹中,这可能很危险。
  2. init 命令确实有一个 -upgrade 参数,以便在约束限制内升级提供程序版本。

使用terraform init -upgrade命令升级每个提供者的最新可接受版本。

升级前

ubuntu@staging-docker:~/terraform$ terraform -version
Terraform v0.12.8
+ provider.aws v2.16.0
+ provider.template v2.1.2

命令升级

ubuntu@staging-docker:~/terraform$ terraform init -upgrade
Upgrading modules...
- asg in asg
- ecs in ecs
- lambda in lambda
- lt in lt

Initializing the backend...

Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "aws" (hashicorp/aws) 2.27.0...
- Downloading plugin for provider "template" (hashicorp/template) 2.1.2...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.aws: version = "~> 2.27"
* provider.template: version = "~> 2.1"

升级后

ubuntu@staging-docker:~/terraform$ terraform version
Terraform v0.12.8
+ provider.aws v2.27.0
+ provider.template v2.1.2