将升级至 Terraform 0.12 版本
公式文档
请参考官方文档,其中有详细说明。
如果您能够耐心进行操作,请按照官方文档顺序进行操作。
(这个备注也是根据官方文档进行操作的)
升级到 Terraform v0.12
概述
-
- 0.12からHCLの構文が変わる
-
- アップグレード前に実行するチェックリストツールが用意されている
- 自動アップグレードツールが用意されている
步骤
-
- 准备 Terraform v0.11.14 和 v0.12.0 的版本
-
- 将 tf 文件更新为 v0.11.14 兼容版本
通过执行 terraform apply 确保没有差异
使用 v0.11.14 升级前的检查清单工具进行检查
升级提供程序版本(到 0.12 兼容版)
更改 tf 文件的版本并执行 terraform init
执行自动升级工具,升级到 v0.12.0
在中文中,可以如下表达:「步骤备忘录」
请准备好Terraform v0.11.14和v0.12.0的版本。
我下载了两个版本并将它们作为terraform11和terraform12放置在当前目录中。
https://releases.hashicorp.com/terraform/0.12.0/
https://releases.hashicorp.com/terraform/0.11.14/
也可以使用tfenv。
使用v0.11.14升级前的检查清单工具进行检查。
没问题
$ ~/terraform11 0.12checklist
Looks good! We did not detect any problems that ought to be
addressed before upgrading to Terraform v0.12.
This tool is not perfect though, so please check the v0.12 upgrade
guide for additional guidance, and for next steps:
https://www.terraform.io/upgrade-guides/0-12.html
需要将 template 提供程序更新至兼容版本。
$ ~/terraform11 0.12checklist
After analyzing this configuration and working directory, we have identified some necessary steps that we recommend you take before upgrading to Terraform v0.12:
- [ ] Upgrade provider "template" to version 2.1.2 or newer.
No currently-installed version is compatible with Terraform 0.12. To upgrade, set the version constraint for this provider as follows and then run `terraform init`:
version = "~> 2.1.2"
Taking these steps before upgrading to Terraform v0.12 will simplify the upgrade process by avoiding syntax errors and other compatibility problems.
执行 v0.12.0 的自动升级工具。
需要将Google提供程序更新到兼容版本。
$ ~/terraform12 0.12upgrade
This command will rewrite the configuration files in the given directory so
that they use the new syntax features from Terraform v0.12, and will identify
any constructs that may need to be adjusted for correct operation with
Terraform v0.12.
We recommend using this command in a clean version control work tree, so that
you can easily see the proposed changes as a diff against the latest commit.
If you have uncommited changes already present, we recommend aborting this
command and dealing with them before running this command again.
Would you like to upgrade the module in the current directory?
Only 'yes' will be accepted to confirm.
Enter a value: yes
-----------------------------------------------------------------------------
Error: error resolving providers:
- provider.google: no suitable version installed
version requirements: "2.6.0"
versions installed: none
在更改tf文件的版本之前,先执行terraform init。
$ ~/terraform12 init
Initializing the backend...
Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "google" (terraform-providers/google) 2.7.0...
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.
升级成功
$ ~/terraform12 0.12upgrade
This command will rewrite the configuration files in the given directory so
that they use the new syntax features from Terraform v0.12, and will identify
any constructs that may need to be adjusted for correct operation with
Terraform v0.12.
We recommend using this command in a clean version control work tree, so that
you can easily see the proposed changes as a diff against the latest commit.
If you have uncommited changes already present, we recommend aborting this
command and dealing with them before running this command again.
Would you like to upgrade the module in the current directory?
Only 'yes' will be accepted to confirm.
Enter a value: yes
-----------------------------------------------------------------------------
Upgrade complete!
The configuration files were upgraded successfully. Use your version control
system to review the proposed changes, make any necessary adjustments, and
then commit.