LoginSignup
55
17

More than 3 years have passed since last update.

terraform 0.14.0 terraform.lock.hclはレビュー時に理解必須なので要約してみた

Last updated at Posted at 2020-12-06

terraform.lock.hclとは

0.14.0がGAになった記事にあるように、terraform init時に作成されるようになったファイルです。

役割は、Terraform が作業ディレクトリの .terraform サブディレクトリにキャッシュする様々なアイテムのロックファイルとのこと。

まず思ったのがこの新しいファイルはバージョン管理に含めるべきなのか?ですが、必要とのことでした。

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.

和訳

Terraform 0.14で起動する依存性ロックファイルは、Terraform initの実行時に自動的に生成されます。生成されたロックファイルはバージョン管理システムにコミットされ、Terraformが将来の実行時に完全に同じプロバイダのバージョンを選択できるようになります。

構造

terraformファイル内のprovider requirement(verison指定など)が存在する場合、このロックファイルに記録される。

  • version: 決定されたプロバイダーバージョン

  • constraints: terraformが実際には参照しない値。人間向けに、version決定についての制限を示す。

  • hashes: New provider package checksumsという機能のhash値。

New provider package checksums

  • providerのブロックの変更がないにもかかわらず、以前は記録されていなかった新しいチェックサムが追加されることがあります。
  • h1, zhの接頭辞は、異なるハッシュスキームを示している

    • 新しいチェックサムが追加されることは、Terraformが異なるハッシュ方式の間で徐々に移行していることを表しています
      • なぜこれが発生するかというのは詳しい内部仕様理解が必要っぽいが、とりあえず都合が良い場合に新しいハッシュスキームを導入するらしい
  • 新しいハッシュスキームが導入されるのが都合が悪い場合は、terraform providers lockにて、事前に追加設定など可能。

    • この詳しい挙動は、terraform providers lockのコマンド説明を参照する
    • ちらっと見ると、異なるプラットフォーム上での操作(Mac or Windows)を想定しているよう。そういったユーザーがいないチームにはあまり関係がないのかも。

プロバイダーのアップデート

  • provider requirementに例えばバージョン指定がある場合に、そのバージョンをアップデートする場合には、terraform init -upgradeを実行する必要がある
    • これによって、ロックファイル内にあるversionに新しいバージョンでのロックを記録する

(実行の様子)

$ terraform init -upgrade

Upgrading modules...

- s3 in modules/aws/s3
- security_group in modules/aws/security_group
- vpc_endpoint in modules/aws/vpc_endpoint

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/template...
- Finding hashicorp/aws versions matching "~> 3.20.0"...
- Installing hashicorp/template v2.2.0...
- Installed hashicorp/template v2.2.0 (signed by HashiCorp)
- Installing hashicorp/aws v3.20.0...
- Installed hashicorp/aws v3.20.0 (signed by HashiCorp)

Terraform has made some changes to the provider dependency selections recorded
in the .terraform.lock.hcl file. Review those changes and commit them to your
version control system if they represent changes you intended to make.

ロックファイルの差分例

  provider "registry.terraform.io/hashicorp/aws" {
-  version     = "3.17.0"
-  constraints = "~> 3.17.0"
+  version     = "3.20.0"
+  constraints = "~> 3.20.0"
55
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
55
17