dshimizu/blog/alpha

とりとめのないITブログ

Terraform 0.12 から 0.13 へバージョンアップした

Terraform 0.13 がリリースされて 1 ヶ月ほどたったので、今使っている 0.12 を 0.13 へバージョンアップした。

事前準備

2020/09 時点で最新版の Terraform v0.13 系をダウンロードする。

Terraform のルートディレクトリへ移動する。 ここではterraformディレクトリであると仮定する。

$ cd terraform

terraform planを実行して差分が出ない状態であることを確認する。 差分が出る場合はapplyしておく。

$ terraform plan

バージョンアップ

まず Terraform のバージョンを 0.13 へ変更する。 required_version が記載されている箇所を変更する。ここではversions.tfに記載されているのでそれを変更する。

terraform {
  required_version = ">= 0.13"
}

バージョンアップのコマンドを実行する。

$ terraform 0.13upgrade
This command will update the configuration files in the given directory to use
the new provider source features from Terraform v0.13. It will also highlight
any providers for which the source cannot be detected, and advise how to
proceed.

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!

terraform initを実行する。すると、以下のようなエラーになる。

$ terraform init
Initializing modules...

Initializing the backend...

Error: Failed to decode current backend config

The backend configuration created by the most recent run of "terraform init"
could not be decoded: unsupported attribute "lock_table". The configuration
may have been initialized by an earlier version that used an incompatible
configuration structure. Run "terraform init -reconfigure" to force
re-initialization of the backend.

バックエンド構成が全バージョンのものであるとのことで、再構成する。

$ terraform init -reconfigure

Initializing the backend...

Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.

Initializing provider plugins...
- Using previously-installed hashicorp/aws v3.6.0
- Using previously-installed -/aws v3.6.0

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, we recommend adding version constraints in a required_providers block
in your configuration, with the constraint strings suggested below.

* -/aws: version = "~> 3.6.0"
* hashicorp/aws: version = "~> 3.6.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.

applyを実行する。

$ terraform apply

手順としてはこれで良い。ただし、以下のようなエラーが出る。

発生したエラー

自分の環境では以下のようなエラーが発生した。

元々 v0.12 のときには s3 のリソース作成時にリージョンを指定するようにしてたが、 v0.13 では以下のようなエラーが出た。 これは v0.13 では region の指定が不要になったためなので削除すれば良い。

Error: Computed attribute cannot be set

  on s3.tf line 48, in resource "aws_s3_bucket" "hogehoge":
  48:   region = "ap-northeast-1"

以下のエラーはなぜ起こったのかわからないが、リソースを追加するように追記した。

Error: Provider configuration not present

To work with aws_ssm_activation.hogehoge its original provider configuration
at provider["registry.terraform.io/-/aws"] is required, but it has been
removed. This occurs when a provider configuration is removed while objects
created by that provider still exist in the state. Re-add the provider
configuration to destroy aws_ssm_activation.lightsail, after which you can
remove the provider configuration again.

参考