Version constraints and the lock file

Intermediate · 8 min

Write provider version constraints, understand what .terraform.lock.hcl records, and see h1 and zh hashes computed from real zips that match a real lock file.

Before thisProviders and the registryInstalling and pinning Terraform and OpenTofu

Jeff Brown Tech · 15 min · from 2:43

Version constraints and the lock file

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Jeff Brown TechReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A version constraint such as `~> 6.0` says which provider versions are acceptable. The lock file records which exact version was chosen, so constraints set a range and the lock file fixes the point.
  • `~> 6.74.0` allows only 6.74.x. `~> 6.0` allows any 6.x from 6.0 up, and never 7.0.
  • `.terraform.lock.hcl` stores a version and a list of hashes per provider. Commit it. `init` reuses it, and `init -upgrade` replaces it with the newest version the constraints allow.
  • `zh:` hashes are SHA-256 of the release zips. `h1:` hashes are computed from the files inside, so they match however the package arrived.
  • Hashes are per platform, so a lock file made on a Mac may lack your CI's Linux hash. `terraform providers lock -platform=...` adds them.

Last month it worked

A team’s pipeline passes on Friday. On Monday, with no change to the code, it fails: the provider shipped a new minor release over the weekend that changed a default. The code did not change. The dependency did, silently, because the constraint said “6.something” and init fetched whatever was newest.

That is the oldest story in package management, and Terraform solves it the same way as everyone else: a range in the code and an exact record in a lock file.

Constraints

The constraint is the version in required_providers, written from the Terraform docs:

required_providers {
  aws = {
    source  = "hashicorp/aws"
    version = "~> 6.0"
  }
}

The operators are =, !=, >, >=, <, <= and ~>, the pessimistic operator. The rightmost number you write may increase and the ones before it are fixed. To check I understood them, I wrote an evaluator and ran it against every release of hashicorp/aws in the registry. It is my stand-in of the documented rules, not Terraform’s own code, and it ignores pre-releases:

hashicorp/aws releases in registry: 514 latest: 6.74.0
~> 6.0               ->  79 match, highest 6.74.0
~> 6.74.0            ->   1 match, highest 6.74.0
>= 5.0, < 6.0        -> 124 match, highest 5.100.0
~> 5.40              ->  73 match, highest 5.100.0
= 4.67.0             ->   1 match, highest 4.67.0
!= 6.74.0, >= 6.70   ->   4 match, highest 6.73.0

~> 6.0 accepts 79 releases, any 6.x. ~> 6.74.0 accepts one. Note that ~> 5.40 reaches 5.100.0: the minor number is allowed to grow, and the last two digits before it are the floor. A comma means and. Providers are conventionally pinned to a major, ~> 6.0, which allows features and fixes but not breaking changes.

The same syntax serves required_version for the CLI itself, covered in the earlier pinning lesson.

What the lock file holds

A constraint is a range, and init chooses a point in it. The choice goes into .terraform.lock.hcl. Here is a real one. HashiCorp’s learn-terraform-variables repository commits its lock file, and this is the random entry from it:

provider "registry.terraform.io/hashicorp/random" {
  version = "3.6.2"
  hashes = [
    "h1:VavG5unYCa3SYISMKF9pzc3718M0bhPlcbUZZGl7wuo=",
    "zh:0ef01a4f81147b32c1bea3429974d4d104bbc4be2ba3cfa667031a8183ef88ec",
    "zh:1bcd2d8161e89e39886119965ef0f37fcce2da9c1aca34263dd3002ba05fcb53",
    ...
  ]
}

Each entry has the provider address, the exact version, the constraints that led to it, if any, and a list of hashes. There is one h1: and twelve zh: in this file. The docs explain them: zh: is the “zip hash”, a SHA-256 of an official release zip, and h1: is a newer scheme computed from the package’s contents, so it matches an official zip, an unpacked directory or a recompressed copy.

Reproducing the hashes

I wanted to know whether I could produce these numbers myself, so I downloaded hashicorp/random 3.6.2 for five platforms from releases.hashicorp.com. For each, I computed the SHA-256 of the zip, and an h1: hash following the documented idea: hash each file inside, list them as hash name, sorted, then hash that text and base64 it. I did not run Terraform. The code is my reconstruction:

darwin_amd64.zip  h1:R5qdQjKz...  zh:0ef01a4f81147b32c...
darwin_arm64.zip  h1:VavG5unYCa3SYISMKF9pzc3718M0bhPlcbUZZGl7wuo=  zh:37c75d15e9514556a...
linux_amd64.zip   h1:wmG0QFjQ...  zh:4210550a767226976...
linux_arm64.zip   h1:UQlmHGdd...  zh:1bcd2d8161e89e398...
windows_amd64.zip h1:5lstwe/L...  zh:f83e925ed73ff8a5e...

The darwin_arm64 h1: is VavG5unYCa3SYISMKF9pzc3718M0bhPlcbUZZGl7wuo=, identical to the one in the committed lock file. Whoever made that lock file was most likely on an Apple Silicon Mac. And all five zip hashes appear among its zh: lines. So the lock file is just a list of checksums of the zips, plus one content hash for the machine that ran init. That is also why a Mac’s lock file lacks a Linux h1:: Terraform only adds the h1: for platforms it installed on.

The CI trap

The consequence is practical. Run terraform init on a Mac, commit the lock file, and CI on Linux has a zh: hash for the Linux zip but no h1:. The docs say Terraform adds h1: values as it confirms matching packages, so the lock file ends up changed in CI, and each person’s first run on a new platform edits a committed file. I did not run it to see the exact behaviour. The docs recommend pre-filling every platform you use:

terraform providers lock -platform=linux_amd64 -platform=darwin_arm64 -platform=windows_amd64

The docs say this records h1: and zh: hashes for the listed platforms. Written from the docs, not run.

Upgrading on purpose

By default init reuses what the lock file says. To move forward, run terraform init -upgrade. Terraform picks the newest version your constraints allow and rewrites the entry. Then review the diff, run a plan and commit the file in a pull request. That is the point of committing it. A provider upgrade becomes a change someone reviews, not something that happens on Monday. Upgrading Terraform and providers builds a routine around this.

OpenTofu and the hashes

The same version number from OpenTofu’s registry is a different build. For hashicorp/random 3.6.2 on darwin arm64, I downloaded OpenTofu’s zip and computed the hashes the same way:

HashiCorp build:  h1:VavG5unYCa3SYISMKF9pzc3718M0bhPlcbUZZGl7wuo=
OpenTofu build:   h1:cwVnVdQqyli6MhRE74KtT70s6MepGHFFQu+oKcbETP4=

Different hashes, different signing keys and different filenames inside the zip: HashiCorp’s contains terraform-provider-random_v3.6.2_x5 and a LICENSE.txt, OpenTofu’s has terraform-provider-random, LICENSE and README.md. So a lock file produced by one tool will not list the other’s hashes. If a team runs both, expect the lock file to need entries from both, and test it. I did not run tofu init against a Terraform-made lock file, so I cannot show what it prints.

About the video

The embed is the “Provider Versioning” chapter of Jeff Brown Tech’s “Terraform for Beginners | Providers & State File”, December 2025, from 2:43 to 10:40. Its description says it covers provider versioning, lock files and the state file. I judged it from that and the chapter list. The hash reproduction above is mine, not the video’s. Zeal Vora’s 2024 video in deeper covers constraints, upgrading and signatures in 11 minutes. English was judged from title, channel and English auto-captions.

Where this fits

Version control for plugins, done. The remaining question for providers is how they get credentials without keys in your repo: authenticating providers without static keys.

Check yourself

  1. 01A constraint says `~> 6.0` and the lock file says 6.62.0. Registry has 6.74.0. Which version does `terraform init` install?

    6.62.0. `init` honours the lock file and only picks a new version when you run `init -upgrade`, which chooses the newest version the constraints allow.

  2. 02What is the difference between `~> 6.0` and `~> 6.0.0`?

    `~> 6.0` allows 6.0 up to but not including 7.0, so any 6.x. `~> 6.0.0` allows only 6.0.x. The rightmost number you write is the one allowed to increase.

  3. 03Your lock file works on your Mac, but CI on Linux reports a checksum problem. What happened?

    The lock file has hashes for your Mac's platform only. Run `terraform providers lock -platform=linux_amd64 -platform=darwin_arm64` and commit the result.

  4. 04Why should the lock file be committed when it is generated?

    It makes a provider upgrade a reviewable change in a pull request, and it makes every machine and the CI runner install exactly the same builds.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark