.terraform.lock.hcl

hata published on
4 min, 731 words

Categories: infrastructure

.terraform.lock.hcl🔗

Announcing HashiCorp Terraform 0.14 General Availability

によると 0.14 で保存されるようになったらしい。(0.14 が出たの 2020-12-04 なのでちょうど一年くらい前か)

terraform init で生成され、リポジトリにコミットすべきもの。 なぜなら、これによって、terraform の実行環境が変わっても Provider の挙動に同一性が確保されるから。 そのためのバージョンやハッシュなどを保存している。

The dependency lock file launching with Terraform 0.14 is generated automatically when Terraform init is run. The generated lockfile should be committed into version control systems so that Terraform can guarantee to select exactly the same provider versions on future runs.

The generated lockfile should be committed into version control systems so that Terraform can guarantee to select exactly the same provider versions on future runs. 生成されたロックファイルはバージョン管理システムにコミットし、Terraformが今後の実行時に全く同じプロバイダーのバージョンを選択することを保証できるようにします。

Terraform 0.14 Introduces a Dependency Lock File for Providers

また、こちらによると、

If you don’t want to use this new behavior you can run terraform init -upgrade and discard .terraform.lock immediately after it is created. The lockfile can equally be added to a list of files ignored by version control. この新しい動作を使用したくない場合は、terraform init -upgrade を実行して、.terraform.lock を作成した直後に破棄することができます。ロックファイルは、バージョンコントロールが無視するファイルのリストにも同様に追加できます。

-upgrade でこのロックファイルを無視して最新に更新し直してくれるようだ。

terraform init -h | grep '\-upgrade' -A4
  -upgrade                Install the latest module and provider versions
                          allowed within configured constraints, overriding the
                          default behavior of selecting exactly the version
                          recorded in the dependency lockfile.

最新のモジュールとプロバイダのバージョンをインストールする。 設定された制約の範囲内で許可され、依存関係のロックファイルに記録されているバージョンを正確に選択するというデフォルトの動作を超えて、 設定された制約の範囲内で許可された最新のモジュールとプロバイダのバージョンをインストールする。 依存関係のロックファイルに記録されているバージョンを正確に選択するデフォルトの動作よりも優先されます。

ドキュメントはこれ。 Dependency Lock File (.terraform.lock.hcl) - Configuration Language - Terraform by HashiCorp

h1zh はハッシュスキームを表しているらしく、terraform 自体などの発展のなかで新しいハッシュスキームを導入できるようにするための仕組み。

h1 が現在のところ SHA256 で、 zh が zip hash 。 h1 のほうが後から追加された。

terraform providers lock \
  -platform=linux_arm64 \
  -platform=linux_amd64 \
  -platform=darwin_amd64 \
  -platform=windows_amd64

ということもできるらしい。 指定された4つのプラットフォームすべてで必要なすべてのプロバイダーの公式パッケージをダウンロードして検証し、両方zh:とh1:それぞれのチェックサムをロックファイルに記録する。