kikukawa's diary

都内で活動するシステムエンジニアが書いてます。 興味を持った技術やハマったポイント、自分用メモをつけてます。 最近はweb中心

terraformを0.11から0.12にアップグレード

v0.11.11からv0.12.24にアップグレードしたときのメモです。
classmethodさんの記事を参考にしました。

前提

  • アップグレード前はv0.11.11
  • stateファイルはS3で管理されている
  • macOS Catalina
  • tfenv導入済み

0.12checklistの実行

チェックリストコマンドを実行するために
tfenvで v0.11.14 に上げました。
v0.11.14 にしか存在しないコマンドです。

エラー

問題なければ、下記メッセージが出て終わるはずです。

Looks good! We did not detect any problems that ought to be
addressed before upgrading to Terraform 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:

- [ ] Terraform couldn't reach the Terraform Registry (at `registry.terraform.io`) to determine whether current provider plugins are v0.12-compatible.

  In general, we recommend upgrading to the latest version of each provider before upgrading to Terraform v0.12.

Taking these steps before upgrading to Terraform v0.12 will simplify the upgrade process by avoiding syntax errors and other compatibility problems.

このメッセージでググると、providerをupgradeしたほうがいいというような記事がありましたが、 解決しませんでした。

$ terraform init --upgrade=true

Initializing the backend...

Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...

Error installing provider "aws": Get https://releases.hashicorp.com/terraform-provider-aws/: x509: certificate signed by unknown authority.

Terraform analyses the configuration and state and automatically downloads
plugins for the providers used. However, when attempting to download this
plugin an unexpected error occured.

This may be caused if for some reason Terraform is unable to reach the
plugin repository. The repository may be unreachable if access is blocked
by a firewall.

If automatic installation is not possible or desirable in your environment,
you may alternatively manually install plugins by downloading a suitable
distribution package and placing the plugin's executable file in the
following directory:
    terraform.d/plugins/darwin_amd64

そもそもローカルから registry.terraform.io , releases.hashicorp.comping を打っても返ってきません。

ワークアラウンド

ローカルの問題と判断し、dockerコンテナから 0.12checklist を実行しました。
今度は問題なく通りました。

0.12upgradeの実行

あとは参考記事通りに、 v0.12.24 にあげてから terraform init , terraform 0.12upgrade を実行するだけです。

versions.tf ファイルが作成された

backend.tf を作って、そこに required_version を書いてたのですが、 0.12upgrade 実行あとには versions.tf が作成されてました。 backend.tf から required_version は削除しました。

アップグレード後は force_destroy の差分がでなくなった

今回terraformの対象にしていたのはAWSコンソールから手動で作成されたリソースだったのですが、
import後に、planで差分確認だけしている(applyしてない)状態でした。
planでは aws_iam_user に下記差分が出てました。

force_destroy: "" => "false"

force_destroy は、terraform独自のプロパティで、 AWSコンソール上から手動で作ってから、
terraform import を使って後追いするとこの差分がでるようです。

しかし、v0.12にアップグレード後はこの差分がでなくなりました。