Skip to content

InsightsInfrastructure as Code

Pinning Terraform providers with the dependency lock file

What .terraform.lock.hcl records, why a lock file that only holds your laptop's hashes breaks CI, and how to constrain versions, lock every platform and upgrade providers on purpose rather than by accident.

Published November 7, 20266 min read

A plan that was clean on Friday shows forty changes on Monday, and nobody touched the code. A new provider release changed a default, renamed an attribute or started reporting a value differently, and the pipeline picked it up because nothing told it not to. The same thing happens in reverse when two engineers run terraform init a day apart and plan against different provider versions.

Terraform has two mechanisms against this, and they do different jobs. Version constraints in the configuration say which versions are acceptable. The dependency lock file says which version was actually chosen, and which checksums its package must match. This article covers how to use both, the platform-hash problem that makes lock files fail in CI, and a way to upgrade on purpose.

Constraints and the lock file are different things

Constraints live in code, in the terraform block:

versions.tf
terraform {
  required_version = "~> 1.11"
 
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 6.0"
    }
    random = {
      source  = "hashicorp/random"
      version = "~> 3.6"
    }
  }
}

The ~> operator lets only the right-most version component increase. ~> 6.0 allows 6.1 and 6.10 but not 7.0, and ~> 6.2.0 allows 6.2.1 but not 6.3.0. required_version limits the Terraform CLI itself and does not apply to providers. If you leave version out of a provider, Terraform installs the newest release.

HashiCorp's guidance on where to put which constraint is worth following:

Kind of moduleConstraint style
Root module (a stack you run apply in)~> to set both a lower and an upper bound
Reusable moduleMinimum versions only, such as >= 5.0

A reusable module that pins an upper bound forces every consumer to upgrade on the module's schedule. Terraform treats the root and child constraints as equal and proceeds only if all of them are satisfied, so a minimum in the module and a range in the root compose cleanly. Versioning modules covers the module side.

A constraint is a range, though. Within ~> 6.0 the answer to "which version?" still changes every time a release ships. That is what the lock file answers.

What the lock file records

terraform init writes .terraform.lock.hcl next to the configuration:

.terraform.lock.hcl
provider "registry.terraform.io/hashicorp/aws" {
  version     = "6.2.0"
  constraints = "~> 6.0"
  hashes = [
    "h1:...",
    "zh:...",
  ]
}
  • version is the exact version Terraform selected.
  • constraints records the constraints that were considered. It is informational and does not affect installation.
  • hashes are checksums that an installed package must match, or init fails. h1: is a hash of the package contents, so it matches the zip, an unpacked directory or a recompressed zip. zh: is a hash of the official zip from the registry and is a legacy scheme.

Without -upgrade, terraform init reinstalls the version already in the file. With -upgrade, it ignores the recorded selection and takes the newest version that satisfies your constraints. Commit the file, so a provider change goes through review like any other change. The lock file covers providers only: modules are not recorded, and Terraform picks the newest module version that fits the constraint, so pin third-party modules to an exact version.

The platform problem

The failure most teams hit goes like this. An engineer on an Apple-silicon laptop runs init and commits the lock file. CI runs on Linux amd64, and init there fails because the package it downloaded does not match any recorded hash, or the pipeline modifies the lock file mid-run and the working tree is dirty afterwards.

When Terraform installs from a registry, it records signed checksums for the packages of other platforms as zh: hashes, but it adds the h1: hash of the package you actually installed. Each new platform that runs init adds its own h1: entry, so the file churns, one platform at a time. Installing from a filesystem or network mirror is worse: only the checksum for the platform where init ran is recorded, so the configuration will not work elsewhere.

The fix is to populate the hashes for every platform you use in one step:

lock-all-platforms.sh
terraform providers lock \
  -platform=linux_amd64 \
  -platform=linux_arm64 \
  -platform=darwin_arm64

Run it without mirror options and it records the official checksums signed by the provider developer. Commit the result. Add the platforms your CI runners and your engineers' machines use, and remember it when you move runners to Arm.

Make CI read the lock file, not write it

A pipeline should install exactly what was reviewed. -lockfile=readonly suppresses lock file changes but still verifies checksums against what is recorded:

.github/workflows/plan.yml
      - run: |
          terraform init -input=false -lockfile=readonly
          terraform plan -input=false -out=tfplan

If a provider is missing a hash for the runner's platform, or a constraint no longer matches the recorded version, init fails and the engineer fixes the lock file in a pull request. That is the behaviour you want. The flag cannot be combined with -upgrade.

The same applies to scheduled drift detection and to plans for many accounts: each root module has its own lock file, so a fleet of stacks has a fleet of lock files, and a shared provider bump is a change in every one of them.

Upgrading on purpose

Upgrades should be a pull request that does nothing else:

upgrade-providers.sh
terraform init -upgrade
terraform providers lock \
  -platform=linux_amd64 \
  -platform=linux_arm64 \
  -platform=darwin_arm64
terraform plan
git diff .terraform.lock.hcl
  1. Widen the constraint if needed. A major version needs an edit to versions.tf, which is the point: it is a visible decision.
  2. Run init -upgrade, then re-run providers lock so that every platform has hashes again.
  3. Read the plan. A provider upgrade that changes resources is a finding. Read the provider's changelog and upgrade guide, then fix the code rather than accepting the diff.
  4. Merge one stack first, usually a non-production one, and let the others follow.

A dependency update bot can open these pull requests, but it needs to understand lock files and must not replace a hash list with the hashes of the platform it ran on. If a bot's changes keep breaking CI, check the lock file diff for lost h1: entries before blaming the provider. See automated dependency updates for keeping that kind of automation useful.

What goes wrong

  • The lock file is in .gitignore. This is common in templates, and it makes every pipeline run an unreviewed upgrade within your constraints. Remove it from the ignore list.
  • Unpinned providers in a module. A provider used by a module but not declared in required_providers still gets installed, with no constraint and the newest version. Declare every provider, including random and null.
  • Shared plugin cache surprises. A plugin cache directory speeds up init, but it is not an authoritative source of hashes. Treat the lock file as the record, not the cache.
  • Removed providers. From Terraform 1.0 on, init drops entries for providers no longer referenced in configuration or state. Older versions left them behind, which could cause "missing or corrupted provider plugins" errors after an upgrade.
  • One lock file per directory. A change in one root module's lock file says nothing about another's. Two stacks that share a module can end up on different provider versions for months.

Where to start

  1. Make sure .terraform.lock.hcl is committed in every root module, and not ignored.
  2. Add required_version and a ~> constraint for every provider in each root module, and minimums only in reusable modules.
  3. Run terraform providers lock with a -platform flag for every OS and architecture that runs Terraform, and commit the result.
  4. Switch CI to terraform init -lockfile=readonly.
  5. Make provider upgrades a separate pull request that runs init -upgrade, refreshes the hashes and shows its plan.

Sources: HashiCorp documentation for the dependency lock file, terraform providers lock, terraform init and version constraints.