dshimizu/blog/alpha

とりとめのないITブログ

Terraform 0.13 から 1.1 へアップグレードした

はじめに

個人プロジェクトだけども、いまだに Terraform 0.13 系を使っていたけどもう 1.1 まで出ているのでアップグレードしようと思ってやってみた。 0.13 から 1.1 へは直接アップグレードできず、段階的にアップグレードする必要があったのでその辺りの手順を一応まとめておく。

tfevn は使っておらず、docker イメージを使っているので、各バージョンの terraform のイメージを取得して実行している。 0.13 -> 0.14.11 の時だけちょっとハマったが、大したことやっていなかったのもあるけど普通にアップグレードできた。

バージョンアップ

0.13 -> 0.14.11

0.13 から 0.14.11 へアップグレードしようとすると以下のエラーが出る。

%  docker run -it --rm -v $PWD:/work -w /work -e AWS_ACCESS_KEY_ID=`aws --profile default configure get aws_access_key_id` -e AWS_SECRET_ACCESS_KEY=`aws --profile default configure get aws_secret_access_key` hashicorp/terraform:0.14.11 init -upgrade
    :
Error: Invalid legacy provider address

This configuration or its associated state refers to the unqualified provider
"aws".

You must complete the Terraform 0.13 upgrade process before upgrading to later
versions.

provider の指定の仕方(?)が変わっているようなので、state replace-provider で置き換える。

%  docker run -it --rm -v $PWD:/work -w /work -e AWS_ACCESS_KEY_ID=`aws --profile default configure get aws_access_key_id` -e AWS_SECRET_ACCESS_KEY=`aws --profile default configure get aws_secret_access_key` hashicorp/terraform:0.14.11 state replace-provider "registry.terraform.io/-/aws" "hashicorp/aws"
Terraform will perform the following actions:

  ~ Updating provider:
    - registry.terraform.io/-/aws
    + registry.terraform.io/hashicorp/aws

Changing XX resources:

  data.aws_caller_identity.current
  aws_iam_access_key.xxxx
  aws_iam_policy.xxxx
  aws_iam_user.xxxx
  aws_iam_user_login_profile.xxxx
  aws_iam_group.xxxx
  aws_iam_group_membership.xxxx
  aws_iam_group_policy_attachment.xxxx
  data.aws_iam_policy_document.xxxx
    :

Do you want to make these changes?
Only 'yes' will be accepted to continue.

Enter a value: yes

Successfully replaced provider for X resources.

再度アップグレードを試す。

% docker run -it --rm -v $PWD:/work -w /work -e AWS_ACCESS_KEY_ID=`aws --profile default configure get aws_access_key_id` -e AWS_SECRET_ACCESS_KEY=`aws --profile default configure get aws_secret_access_key` hashicorp/terraform:0.14.11 init -upgrade

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Using previously-installed hashicorp/aws v4.9.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.

いけた。 差分を確認し、必要に応じて修正する。

% docker run -it --rm -v $PWD:/work -w /work -e AWS_ACCESS_KEY_ID=`aws --profile default configure get aws_access_key_id` -e AWS_SECRET_ACCESS_KEY=`aws --profile default configure get aws_secret_access_key` hashicorp/terraform:0.14.11 plan -var-file=terraform.tfvars

0.14.11 -> 0.15.5

0.15.5 の Terraform でアップグレードを行う。

% docker run -it --rm -v $PWD:/work -w /work -e AWS_ACCESS_KEY_ID=`aws --profile default configure get aws_access_key_id` -e AWS_SECRET_ACCESS_KEY=`aws --profile default configure get aws_secret_access_key` hashicorp/terraform:0.15.5 init -upgrade

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Using previously-installed hashicorp/aws v4.9.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.

差分を確認し、必要に応じて修正する。

% docker run -it --rm -v $PWD:/work -w /work -e AWS_ACCESS_KEY_ID=`aws --profile default configure get aws_access_key_id` -e AWS_SECRET_ACCESS_KEY=`aws --profile default configure get aws_secret_access_key` hashicorp/terraform:1.0.11  plan -var-file=terraform.tfvars

0.15.5 -> 1.0.11

1.0.11 の Terraform でアップグレードを行う。

% docker run -it --rm -v $PWD:/work -w /work -e AWS_ACCESS_KEY_ID=`aws --profile default configure get aws_access_key_id` -e AWS_SECRET_ACCESS_KEY=`aws --profile default configure get aws_secret_access_key` hashicorp/terraform:1.0.11 init --upgrade

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Using previously-installed hashicorp/aws v4.9.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.

差分を確認し、必要に応じて修正する。

% docker run -it --rm -v $PWD:/work -w /work -e AWS_ACCESS_KEY_ID=`aws --profile default configure get aws_access_key_id` -e AWS_SECRET_ACCESS_KEY=`aws --profile default configure get aws_secret_access_key` hashicorp/terraform:1.0.11  plan -var-file=terraform.tfvars

1.0.11 -> 1.1.3

1.1.3 の Terraform でアップグレードを行う。

% docker run -it --rm -v $PWD:/work -w /work -e AWS_ACCESS_KEY_ID=`aws --profile default configure get aws_access_key_id` -e AWS_SECRET_ACCESS_KEY=`aws --profile default configure get aws_secret_access_key` hashicorp/terraform:1.1.3 init --upgrade
Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Using previously-installed hashicorp/aws v4.9.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.

差分を確認し、必要に応じて修正する。

% docker run -it --rm -v $PWD:/work -w /work -e AWS_ACCESS_KEY_ID=`aws --profile default configure get aws_access_key_id` -e AWS_SECRET_ACCESS_KEY=`aws --profile default configure get aws_secret_access_key` hashicorp/terraform:1.1.3 plan -var-file=terraform.tfvars

まとめ

個人プロジェクトで Terraform 0.13 系を使っていたけど 1.1 が出ているのでアップグレードした際の内容をメモがてら記載した。 大したことをやっていないので、ほぼ差分も出ずバージョンアップできた。実際には複数の環境があったりいろんな定義があったりするので、こんなに一気にはいけないだろうな...と思った。

参考