はじめに
Terraform v0.12.29で作成したリソースを更新しようとしたところ、
リモートのstateファイルのproviderが更新できず解決に四苦八苦したため、
解決方法を残します。
実行環境
- macOS Big Sur 11.4
- Terraform v0.14.3
経緯
半年ほど前にterraformで作成したAWSリソースを更新する機会があったのですが、
いざtfファイルを変更してterraform planを実行したところ
Error: Could not load plugin
Plugin reinitialization required. Please run "terraform init".
Plugins are external binaries that Terraform uses to access and manipulate
resources. The configuration provided requires plugins which can't be located,
don't satisfy the version constraints, or are otherwise incompatible.
Terraform automatically discovers provider requirements from your
configuration, including providers used in child modules. To see the
requirements and constraints, run "terraform providers".
プラグインが読み込めないとエラーが出ました。
terraform initを再実行しろと言われているので実行したところ
Error: Invalid legacy provider address
This configuration or its associated state refers to the unqualified provider
"archive".
You must complete the Terraform 0.13 upgrade process before upgrading to later
versions.
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にawsと、Lambdaのzip用にarchiveを利用していました)。
Terraform 0.13 にアップグレードしろと言われているものの、実行時のバージョンは0.14.3です。
stateファイルはS3で管理していたので、とりあえず.terraformディレクトリを削除して
再度terraform initしてみましたが、結果は変わらず。
そこでS3に保存しているtfstateを確認したところ、"terraform_version": "0.12.29"となっていました。
なるほどリモートのstateファイルが更新できていなかったようです。
解決方法
どうやらv0.13からproviderの送信元アドレスの指定方法が変更されたらしいので、
以下のコマンドでstateファイルのproviderを更新しました。
terraform state replace-provider registry.terraform.io/-/aws hashicorp/aws
terraform state replace-provider registry.terraform.io/-/archive hashicorp/archive
その後terraform initで無事terraformのバージョンも更新でき、
terraform applyも実行できました!
最後に
どうやらv0.12.29からv0.13にアップグレードするときに踏まないといけない手順だったようで、
公式のv0.13アップグレードガイドにしっかり書いてましたね、、、
余談ですが、v0.13にアップグレードしたことでtfファイルを変更していないのに差分が出るリソースもあるみたいなので、
意図しない変更が加わっていないか注意しておきましょう。