Skip to main content

Infrastructure as code · Beginner · 18 min

Pin Terraform provider versions and read the dependency lock file

By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · Terraform 1.17.2 · hashicorp/random 3.9.1 · hashicorp/local 2.8.0

You will pin two providers with version constraints, read every line of the lock file Terraform writes, watch init refuse a mismatch, upgrade on purpose, record hashes for other platforms and share downloads through a plugin cache.

On this page

A provider is the plugin Terraform downloads to talk to an API: AWS, Cloudflare, or in this tutorial, the random and local providers that run entirely on your machine. Providers ship new versions often. Without a pin, a teammate who runs terraform init next month can get a different version than you did, and a plan that worked yesterday can change today with no edit to your code.

Terraform guards against that in two layers. Version constraints in required_providers say which versions you will accept. The dependency lock file, .terraform.lock.hcl, records the exact version Terraform picked and checksums for it, so every later init installs the same build until you choose to move.

In this tutorial you build a small project in ~/lockfile-demo that writes a release name to a file. You will:

  • write three kinds of version constraint: ~>, an exact version, and a >= range
  • run init and read the lock file line by line, including its h1: and zh: hashes
  • commit the lock file to Git and keep .terraform out of it
  • change a constraint, and watch plan and init refuse it until you upgrade
  • upgrade on purpose with init -upgrade and review the diff
  • add hashes for teammates on macOS, Windows and ARM Linux with terraform providers lock
  • share provider downloads between projects with a plugin cache

Nothing touches a cloud account and nothing bills. The same files and commands work in OpenTofu if you swap terraform for tofu.

Step 1 · Install Terraform 1.17 and Git

Install curl, unzip and git, then download Terraform 1.17.2 for your machine's architecture, check it against HashiCorp's published checksums and put the binary on your PATH:

Bash
sudo apt update
sudo apt install -y curl unzip git ca-certificates
TF_VERSION=1.17.2
ARCH=$(dpkg --print-architecture)
cd "$(mktemp -d)"
curl -fsSLO "https://releases.hashicorp.com/terraform/${TF_VERSION}/terraform_${TF_VERSION}_linux_${ARCH}.zip"
curl -fsSLO "https://releases.hashicorp.com/terraform/${TF_VERSION}/terraform_${TF_VERSION}_SHA256SUMS"
sha256sum --ignore-missing -c "terraform_${TF_VERSION}_SHA256SUMS"
unzip -o "terraform_${TF_VERSION}_linux_${ARCH}.zip" terraform
sudo install -m 0755 terraform /usr/local/bin/terraform
terraform version

A binary installed this way gets no updates from apt. To upgrade Terraform itself, run the same commands with a newer TF_VERSION.

Check it worked

sha256sum prints terraform_1.17.2_linux_amd64.zip: OK (or arm64), and terraform version prints Terraform v1.17.2 on its first line.

Step 2 · Write version constraints and run init

Create the project. versions.tf holds every constraint, so anyone reviewing a version change only has to read one file:

Bash
mkdir -p ~/lockfile-demo/app
cd ~/lockfile-demo/app

cat > versions.tf <<'HCL'
terraform {
  # Any 1.17.x release, never 1.18.
  required_version = "~> 1.17.0"

  required_providers {
    random = {
      source  = "hashicorp/random"
      # Any 3.8.x release: patch updates only.
      version = "~> 3.8.0"
    }
    local = {
      source  = "hashicorp/local"
      # Exactly this version and no other.
      version = "2.8.0"
    }
  }
}
HCL

cat > main.tf <<'HCL'
resource "random_pet" "release" {
  length = 2
}

resource "local_file" "release_note" {
  filename = "${path.module}/out/release.txt"
  content  = "Release name: ${random_pet.release.id}\n"
}

output "release_name" {
  value = random_pet.release.id
}
HCL

terraform init
terraform version

The three forms you will meet most:

  • ~> 3.8.0 lets only the last number you wrote move: 3.8.1 and 3.8.9 are allowed, 3.9.0 is not. ~> 3.8 (two numbers) would allow 3.9 and 3.10, but not 4.0.
  • 2.8.0 with no operator (or = 2.8.0) means that one version.
  • >= 2.8.0, < 3.0.0 is a range. A comma means "and". You can also use != to skip one known-bad release.

HashiCorp's advice is to use ~> in root configurations like this one, so upgrades are deliberate, and only a minimum (>= 2.8.0) in reusable modules, so a module never blocks the root from upgrading.

Apply once so there is real state that later upgrades must keep working with:

Bash
terraform plan -out=tfplan
terraform apply tfplan
cat out/release.txt

Check it worked

init prints Installing hashicorp/random v3.8.1... and Installing hashicorp/local v2.8.0..., then says it created a lock file called .terraform.lock.hcl. terraform version lists + provider registry.terraform.io/hashicorp/random v3.8.1 and + provider registry.terraform.io/hashicorp/local v2.8.0. The apply ends with Resources: 2 added, and out/release.txt shows something like Release name: tender-falcon.

Step 3 · Read the lock file line by line

Print the lock file and the folder init filled:

Bash
cd ~/lockfile-demo/app
cat .terraform.lock.hcl
find .terraform/providers -maxdepth 4 -mindepth 4
grep -c '"zh:' .terraform.lock.hcl
grep -c '"h1:' .terraform.lock.hcl

Each provider gets one block. Shortened, the random block looks like this:

HCL
provider "registry.terraform.io/hashicorp/random" {
  version     = "3.8.1"
  constraints = "~> 3.8.0"
  hashes = [
    "h1:Abc...=",
    "zh:0a1b...",
    "zh:1c2d...",
    # ...one zh: line per platform build
  ]
}

Line by line:

  • provider "registry.terraform.io/hashicorp/random" is the full source address. hashicorp/random in your config is short for it.
  • version is the exact version selected. This is the line that pins you, not the constraint in versions.tf.
  • constraints copies the constraints Terraform saw when it chose. If several modules ask for the same provider, they are combined here.
  • h1: is a hash of the unpacked provider files for one platform. init records it for the platform it ran on, here linux_amd64 or linux_arm64.
  • zh: is the SHA-256 of a release zip, taken from the checksums file the provider's author signed. The registry publishes one per platform, so init records a zh: line for every build, including platforms you have never used.

On every later init, Terraform downloads the locked version and refuses it unless it matches one of these hashes. A provider swapped on a mirror or in transit fails that check.

The file only covers providers. It does not record the Terraform version (that is required_version's job) or module versions, so pin modules with an exact version or a Git tag.

Check it worked

The file has two provider blocks, random at 3.8.1 and local at 2.8.0. find prints one folder per provider, ending in your platform, such as .terraform/providers/registry.terraform.io/hashicorp/random/3.8.1/linux_amd64. The zh: count is much larger than the h1: count, which is 2: one h1: per provider for your platform.

Step 4 · Commit the lock file and ignore .terraform

The two outputs of init go to Git in opposite ways:

  • .terraform.lock.hcl is small text that pins versions for everyone. Commit it, and review changes to it like code.
  • .terraform/ holds the downloaded provider binaries for your platform only, often tens of megabytes, and init can rebuild it any time. Never commit it.

State files and plan files stay out too. Create a throwaway repository with a local identity (replace the name and email if you like):

Bash
cd ~/lockfile-demo/app
cat > .gitignore <<'TXT'
.terraform/
*.tfstate
*.tfstate.*
tfplan
out/
TXT
git init -q
git config user.name "Lockfile Demo"
git config user.email "lockfile-demo@example.com"
git add .gitignore versions.tf main.tf .terraform.lock.hcl
git commit -q -m "Pin random ~> 3.8.0 and local 2.8.0"
git status --short
git ls-files

Check it worked

git status --short prints nothing: everything is either committed or ignored. git ls-files lists exactly four files: .gitignore, .terraform.lock.hcl, main.tf and versions.tf.

Step 5 · See init refuse a provider that does not match the lock

Say a colleague asks for a feature from random 3.9. Change the constraint, keeping the lock file as it is:

Bash
cd ~/lockfile-demo/app
cat > versions.tf <<'HCL'
terraform {
  # Any 1.17.x release, never 1.18.
  required_version = "~> 1.17.0"

  required_providers {
    random = {
      source  = "hashicorp/random"
      # Any 3.9.x release: patch updates only.
      version = "~> 3.9.0"
    }
    local = {
      source  = "hashicorp/local"
      # Exactly this version and no other.
      version = "2.8.0"
    }
  }
}
HCL

Now try a plan, then a plain init:

Bash
terraform plan
terraform init

Neither command changes anything. plan stops before talking to any provider, and init will not quietly replace a locked version. Moving a provider is something you ask for, which is the next step.

Check it worked

plan fails with Error: Inconsistent dependency lock file and says the locked version 3.8.1 does not match the updated constraint ~> 3.9.0. init fails with a message saying the locked provider does not match configured version constraint and that you must use terraform init -upgrade. .terraform.lock.hcl still says 3.8.1.

Step 6 · Upgrade deliberately with init -upgrade

-upgrade tells init to ignore the locked versions and pick the newest version each constraint allows:

Bash
cd ~/lockfile-demo/app
terraform init -upgrade
git --no-pager diff .terraform.lock.hcl
terraform plan

Read the diff before you commit. The random block's version, constraints and every hash change. The local block does not move at all: its constraint is an exact 2.8.0, so even with -upgrade there was nothing else to pick. That is the trade-off of an exact pin: safe, but you have to edit it by hand to get fixes.

A provider upgrade can change how existing resources are read, so a plan straight after is your check that nothing will be replaced. When the plan is clean, commit both files together, so the history shows why the lock changed:

Bash
git add versions.tf .terraform.lock.hcl
git commit -q -m "Upgrade hashicorp/random to ~> 3.9.0"
git log --oneline

Check it worked

init prints Installing hashicorp/random v3.9.1... and Using previously-installed hashicorp/local v2.8.0. The diff shows - version = "3.8.1" and + version = "3.9.1" in the random block only. The plan ends with No changes. Your infrastructure matches the configuration., and git log --oneline shows two commits.

Step 7 · Record hashes for teammates on other platforms

Your lock file has an h1: hash for your own platform only. When a teammate on a Mac or Windows laptop runs init, Terraform can still check the download against the zh: lines, but it then adds an h1: line for their platform. Their next commit carries a lock file change nobody asked for. Worse, anything that installs providers from unpacked files, such as the plugin cache in the next step or an internal mirror, can only check h1: hashes, so it cannot verify their platform at all.

terraform providers lock fixes that ahead of time. It downloads each locked provider for every platform you name and records its h1: hash. Name the platforms your team and your CI actually use:

Bash
cd ~/lockfile-demo/app
terraform providers lock \
  -platform=linux_amd64 \
  -platform=linux_arm64 \
  -platform=darwin_arm64 \
  -platform=windows_amd64
grep -c '"h1:' .terraform.lock.hcl
git --no-pager diff --stat

It does not change any versions; it only adds hashes. Commit the result:

Bash
git commit -q -am "Record provider hashes for Linux, macOS and Windows"

The command downloads every provider once per platform, several megabytes each. On prepaid or metered mobile data, run it on a fixed connection or let CI run it, and only list platforms someone really uses.

Check it worked

For each provider, the command prints lines such as - Obtained hashicorp/random checksums for darwin_arm64, then Success! Terraform has updated the lock file. The h1: count goes from 2 to 8 (two providers times four platforms). git diff --stat shows .terraform.lock.hcl as the only changed file, with lines added and none removed.

Step 8 · Share provider downloads with a plugin cache

By default every project folder downloads its own copy of each provider into its own .terraform. With ten projects that use the AWS provider, that is ten copies of a large binary, downloaded ten times. A plugin cache keeps one copy per version and links it into each project.

Turn it on in the Terraform CLI config file, ~/.terraformrc. The command appends one line, so any settings you already keep there stay put. The cache folder has to exist first:

Bash
mkdir -p ~/.terraform.d/plugin-cache
echo 'plugin_cache_dir = "$HOME/.terraform.d/plugin-cache"' >> ~/.terraformrc
cat ~/.terraformrc

$HOME in single quotes is deliberate: Terraform expands it itself. Setting the TF_PLUGIN_CACHE_DIR environment variable does the same job, which suits CI runners.

Rebuild the project's .terraform so its providers land in the cache, then clone the repository as a teammate would and initialise the clone:

Bash
cd ~/lockfile-demo/app
rm -rf .terraform
terraform init
find ~/.terraform.d/plugin-cache -maxdepth 4 -mindepth 4
git clone -q ~/lockfile-demo/app ~/lockfile-demo/teammate
cd ~/lockfile-demo/teammate
ls -A
terraform init
ls -l .terraform/providers/registry.terraform.io/hashicorp/random/3.9.1

The clone holds only what was committed: the code and the lock file, no .terraform. Its init reads the locked versions, finds them in the cache, checks them against the h1: hashes you recorded in the last step, and links them in without a download.

Terraform only takes a provider from the cache when the lock file already lists it, so it can verify the copy. That is one more reason to commit the lock file: a fresh project with no lock still downloads from the registry the first time.

Check it worked

The first init installs both providers into ~/.terraform.d/plugin-cache, and find lists registry.terraform.io/hashicorp/random/3.9.1/... and registry.terraform.io/hashicorp/local/2.8.0/... under it. ls -A in the clone shows .git, .gitignore, .terraform.lock.hcl, main.tf and versions.tf, and no .terraform. The clone's init reports both providers from the shared cache directory, and ls -l .terraform/providers/registry.terraform.io/hashicorp/random/3.9.1 shows a link pointing into the cache.

Step 9 · Clean up

Destroy what the project created. Plan the destroy and read it before applying:

Bash
cd ~/lockfile-demo/app
terraform plan -destroy -out=tfplan
terraform apply tfplan

The next command deletes the whole demo folder, including both copies of the repository and the state. Check you are deleting ~/lockfile-demo and nothing else:

Bash
cd ~
rm -rf ~/lockfile-demo

Keep the plugin cache if you will use Terraform again; it saves a download on every new project. Otherwise remove it. If ~/.terraformrc held only the plugin_cache_dir line, delete the file; if you had other settings in it, remove just that line in an editor instead of running the rm:

Bash
rm -rf ~/.terraform.d/plugin-cache
rm -f ~/.terraformrc

If you installed Terraform only for this tutorial, remove the binary too:

Bash
sudo rm -f /usr/local/bin/terraform

Check it worked

Applying the destroy plan ends with Apply complete! Resources: 0 added, 0 changed, 2 destroyed. ls ~/lockfile-demo then reports No such file or directory, and command -v terraform prints nothing if you removed the binary.

Next steps

  • Run the same workflow with OpenTofu, which reads the same lock file format, in /tutorials/manage-infrastructure-with-opentofu.
  • Weigh the two tools against each other, licence and registry included, in /tutorials/choose-between-terraform-and-opentofu.
  • Catch a missing required_providers block or an unpinned provider before review in /tutorials/lint-terraform-with-tflint-and-scan-it-with-checkov.
  • Run one pinned configuration as separate environments in /tutorials/use-terraform-workspaces-for-separate-environments.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.