継続は力なり

タイトル通り定期的な更新を心掛けるブログです。

Terraform のバージョンを『v0.11』から『v 0.12 』へアップグレード!

タダです.

2019年も残すところ1ヶ月ほど...今年のやり残しは今年のうちに一つずつ片付けたい思いで自分の環境の Terraform のバージョンを v0.11 から v0.12 へアップグレードしたので対応方法を整理しておきます.

www.terraform.io

v0.11 から v0.12 のアップグレードの対処

terraform 0.12checklist コマンド実行の準備

それでは,実際に対処についてまとめていきます.まず,v0.12へ上げるためのプレアップグレードチェックコマンドを実行するためにv0.11.14にバージョンアップします.Terraform のバージョン管理は tfenv で行っています.バージョンアップ後,terraform 0.12checklistを実行してみたらLooks good! We did not detect any problems that ought to be addressed before upgrading to Terraform v0.12 と表示されたので特に問題なくv0.12に上げられることを確認しました.

github.com

$ terraform -v
Terraform v0.11.13

Your version of Terraform is out of date! The latest version
is 0.12.16. You can update by downloading from www.terraform.io/downloads.html

$ tfenv install 0.11.14
[INFO] Installing Terraform v0.11.14
[INFO] Downloading release tarball from https://releases.hashicorp.com/terraform/0.11.14/terraform_0.11.14_darwin_amd64.zip
######################################################################## 100.0%
[INFO] Downloading SHA hash file from https://releases.hashicorp.com/terraform/0.11.14/terraform_0.11.14_SHA256SUMS
tfenv: tfenv-install: [WARN] No keybase install found, skipping GPG signature verification
Archive:  tfenv_download.yGld76/terraform_0.11.14_darwin_amd64.zip
  inflating: /Users/tada/.tfenv/versions/0.11.14/terraform
[INFO] Installation of terraform v0.11.14 successful
[INFO] Switching to v0.11.14
[INFO] Switching completed
$ terraform 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

v0.12へモジュールを変更する

v0.11からv0.12への移行は,Terraform のバージョンだけでなく,Terraform のモジュールもv0.12のものに変更する必要があります.まずは,Terraform のバージョンをv0.12へあげます.その後, tf ファイルがあるディレクトリに移動して terraform 0.12upgradeコマンドでモジュールを上書きします.

All other commands: 0.12upgrade Rewrites pre-0.12 module source code for v0.12

tfenv install 0.12.1
[INFO] Installing Terraform v0.12.1
[INFO] Downloading release tarball from https://releases.hashicorp.com/terraform/0.12.1/terraform_0.12.1_darwin_amd64.zip
######################################################################## 100.0%
[INFO] Downloading SHA hash file from https://releases.hashicorp.com/terraform/0.12.1/terraform_0.12.1_SHA256SUMS
tfenv: tfenv-install: [WARN] No keybase install found, skipping GPG signature verification
Archive:  tfenv_download.RQsRDE/terraform_0.12.1_darwin_amd64.zip
  inflating: /Users/tada/.tfenv/versions/0.12.1/terraform
[INFO] Installation of terraform v0.12.1 successful
[INFO] Switching to v0.12.1
[INFO] Switching completed

$ terraform help
Usage: terraform [-version] [-help] <command> [args]

~中略~
All other commands:
    0.12upgrade        Rewrites pre-0.12 module source code for v0.12
~中略~

Terraform のバージョンを上げたので,続いてterraform 0.12upgradeコマンドを実行してみたところError: failed to load provider "aws": Incompatible API version with plugin. Plugin version: 4, Client versions: [5] と Terraform のプラグインでエラーが出ました.こちらに対応していきます.

$ 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: yes

-----------------------------------------------------------------------------

Error: failed to load provider "aws": Incompatible API version with plugin. Plugin version: 4, Client versions: [5]

Terraform プラグインエラーの対応方法

調べてみたところ,$HOME/.terraform/plugins 配下の terraform-provider-aws_xxxプラグインを削除し,導入し直すコマンドを実行すればエラーを解消できる情報を発見したので試してみたところ,エラーを解消してモジュールのアップグレードも完了しました.

$ rm .terraform/plugins/darwin_amd64/terraform-provider-aws_v2.6.0_x4
$ terraform init -get-plugins=true -reconfigure

Initializing the backend...

Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "aws" (hashicorp/aws) 2.39.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.
blackeyes01-1875 :: ~/terraform_sample » 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: 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.

まとめ

Terraform のバージョンを0.11 から 0.12 へアップグレードした時の対処をまとめました.2019年11月27日時点ではバージョンがv0.12.16になっています.v0.12になると以前と変化点もあるため対処方法を確認して対応していきたいですね.この記事で何か参考になることがあれば幸いです!

www.hashicorp.com

最後にTerraform の勉強でお世話になっている『実践Terraform AWSにおけるシステム設計とベストプラクティス』が安く販売されているようなので検討中の方は今がチャンスです!

関連記事

合わせてこちらの記事もぜひ!

sadayoshi-tada.hatenablog.com

sadayoshi-tada.hatenablog.com