在执行「升级到 Terraform v0.12」时的备忘录记事
首先
主要参考官方文档,升级从Terraform v0.11到v0.12版本的备忘录备忘录。
同时,我们进行了升级的是一个派生自此仓库的仓库。
https://github.com/collectivehealth/terraform-emr-spark-example
程序。
-
- 使用tfenv准备v0.11.14和v0.12系列。
在terraform 0.12checklist中进行升级前的检查。
升级到v0.12系列并执行terraform init。
使用terraform 0.12upgrade进行语法替换。
所有步骤的笔记
使用tfenv来准备v0.11.14和v0.12系列的版本。
使用tfenv能够轻松切换版本,因此我们使用这个工具。将链接为https://github.com/tfutils/tfenv.
$ tfenv install 0.11.14
$ tfenv install 0.12.6
$ tfenv use 0.11.14
$ tfenv list
0.12.6
* 0.11.14 (set by /usr/local/Cellar/tfenv/1.0.1/version)
另外,将tf文件兼容到v0.11.14版本。
使用Terraform 0.12清单进行升级前的检查。
在v0.11.14版本中,有一个名为0.12checklist的命令,可以在升级到v0.12系列之前使用该命令进行预先检查。
$ terraform 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 "aws" to version 2.40.0 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.40.0"
- [ ] Upgrade provider "random" to version 2.2.1 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.2.1"
- [ ] 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"
- [ ] Upgrade provider "tls" to version 2.1.1 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.1"
# Module `"bootstrap"`
# Module `"emr"`
# Module `"lb"`
# Module `"s3"`
# Module `"sec"`
# Module `"sgs"`
Taking these steps before upgrading to Terraform v0.12 will simplify the upgrade process by avoiding syntax errors and other compatibility problems.
由于本次更新中有4个Provider与v0.12系列不兼容,因此按照指示更改了版本限制,并执行了terraform init。
当问题解决后,将显示以下消息。
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
升级至v0.12版本并执行terraform init。
使用tfenv进行版本切换
$ tfenv use 0.12.6
$ tfenv list
* 0.12.6 (set by /usr/local/Cellar/tfenv/1.0.1/version)
0.11.14
执行”terraform init”命令。
$ terraform init
There are some problems with the configuration, described below.
The Terraform configuration must be valid before initialization so that
Terraform can determine which modules and providers need to be installed.
Error: Extraneous label for provider
on config.tf line 5, in provider "aws" "aws":
5: provider "aws" "aws" {
Only 1 labels (name) are expected for provider blocks.
Error: Extraneous label for provider
on config.tf line 10, in provider "random" "random":
10: provider "random" "random" {
Only 1 labels (name) are expected for provider blocks.
Error: Extraneous label for provider
on config.tf line 14, in provider "template" "template":
14: provider "template" "template" {
Only 1 labels (name) are expected for provider blocks.
Error: Extraneous label for provider
on config.tf line 18, in provider "archive" "archive":
18: provider "archive" "archive" {
Only 1 labels (name) are expected for provider blocks.
Error: Extraneous label for provider
on config.tf line 22, in provider "tls" "tls":
22: provider "tls" "tls" {
Only 1 labels (name) are expected for provider blocks.
Error: Extraneous label for provider
on config.tf line 26, in provider "http" "http":
26: provider "http" "http" {
Only 1 labels (name) are expected for provider blocks.
Error: Extraneous label for provider
on config.tf line 30, in provider "local" "local":
30: provider "local" "local" {
Only 1 labels (name) are expected for provider blocks.
由于提供者块的代码不兼容,我们进行了修正。
-provider "aws" "aws" {
+provider "aws" {
使用Terraform 0.12upgrade进行语法替换。
在0.12版本中,有一个名为0.12upgrade的命令,可以使用该命令将代码转换为0.12版本的语法。
$ terraform 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:
输入”是”将执行重写操作。完成后将显示以下消息。
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.
如果已经完成升级,则会出现以下错误。
$ terraform 0.12upgrade
Error: Module already upgraded
on versions.tf line 3, in terraform:
3: required_version = ">= 0.12"
The module in directory . has a version constraint that suggests it has
already been upgraded for v0.12. If this is incorrect, either remove this
constraint or override this heuristic with the -force argument. Upgrading a
module that was already upgraded may change the meaning of that module.
这次可能没有遇到问题,但是有些地方需要手动更改。
此外,0.12 版本升级命令有以下不方便之处。
-
- ファイル末尾に空行が追加されてしまう
https://github.com/hashicorp/terraform/issues/21428
カレントディレクトリのtfファイルしか変更してくれない
実行したディレクトリにversions.tfが作成される
服务商的升级
通过使用 “terraform init” 命令加上 “-upgrade” 选项,可以将其升级到满足版本约束的最新提供商。
$ terraform init -upgrade
Upgrading modules...
- bootstrap in modules/bootstrap
- emr in modules/emr
- lb in modules/lb
- s3 in modules/s3
- sec in modules/sec
- sgs in modules/sgs
Initializing the backend...
Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "archive" (hashicorp/archive) 1.3.0...
- Downloading plugin for provider "tls" (hashicorp/tls) 2.1.1...
- Downloading plugin for provider "http" (hashicorp/http) 1.1.1...
- Downloading plugin for provider "local" (hashicorp/local) 1.4.0...
- Downloading plugin for provider "aws" (hashicorp/aws) 2.40.0...
- Downloading plugin for provider "random" (hashicorp/random) 2.2.1...
- Downloading plugin for provider "template" (hashicorp/template) 2.1.2...
请你在这里找到一种中文的同义表达:
参考一下
由于 Terraform 0.12 发布,所以我试着进行了升级。
https://dev.classmethod.jp/tool/terraform-upgrade-from-0-11-to-0-12/
Terraform职人入门:将日常运维中学到的经验整理梳理