Skip to content

Lesson 3 of 14 · 15m

Init, Providers, and the Lock File

`terraform init` downloads executable provider binaries from a registry — pin versions and commit `.terraform.lock.hcl` or you're trusting the supply chain blind.

terraform init is the only Terraform command that downloads executable code from the internet. Everything else you run works on local files and cloud APIs; init reaches out to registry.terraform.io, resolves your version constraints, and drops Go binaries into .terraform/providers/ — binaries that will execute with your cloud credentials on your machine.

The mental model to kill is "it's a plugin system, latest is fine." A provider is a dependency exactly like an npm package or a vendored library, and it deserves the same suspicion. In March 2024, CVE-2024-3094 — the xz backdoor — nearly shipped a remote-code-execution payload into every Linux SSH server through a compression library nobody was auditing. A Terraform provider gets the same trust-by-download treatment until you pin and checksum it, and it runs with credentials that can delete your VPC.

The mechanics

terraform {
  required_version = "~> 1.10"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.70"
    }
    cloudflare = {
      source  = "cloudflare/cloudflare"
      version = "~> 4.40"
    }
  }
}

source is a registry address: hashicorp/aws means the official AWS provider in HashiCorp's namespace; cloudflare/cloudflare is a verified partner namespace. Anything outside hashicorp/ or a verified partner is community-published — readable source, unvetted binary. Know whose code you're running.

version is a constraint, not a version. ~> 5.70 means >= 5.70, < 6.0 — patch and minor bumps allowed, majors refused. >= 5.0 alone means "any future version," which is how a surprise provider 6.0 with breaking renames lands in a routine apply.

The lock file

The first init writes .terraform.lock.hcl — cryptographic checksums (h1: and zh: hashes) of the exact provider binaries it resolved, per platform. It does three things:

  • init on another machine verifies the downloaded binary against the recorded hashes instead of trusting the registry fresh.
  • init without -upgrade refuses to change provider versions — reproducible installs, the way package-lock.json makes npm ci reproducible.
  • A git diff of the lock file tells you exactly which binaries changed and when.

Commit it. The lock file belongs in the repo next to your .tf files — it's the difference between "everyone runs the same provider" and "everyone runs whichever provider the registry felt like serving today."

One trap for mixed-platform teams: the lock file records hashes for the platform init ran on. If CI runs linux_amd64 and your laptop is darwin_arm64, record both:

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

init also wires up the backend and installs modules — it's the command that turns a directory of .tf files into a working project. Change the backend block and the next init asks whether to migrate state to the new location (-migrate-state), or start fresh against it (-reconfigure). The distinction matters: migrate copies the existing state file; reconfigure abandons it. Pick wrong and your infrastructure is orphaned — not deleted, but unmanaged, with all the import archaeology that implies.

Upgrading is a deliberate act

terraform init -upgrade re-resolves constraints and rewrites the lock file. It's the equivalent of npm update — run it when you intend to bump providers, read the provider changelog first (major releases rename resources; AWS provider 6.x reworked several resources that had been stable for years), and review the lock-file diff in the PR like any other dependency bump.

For air-gapped or distrustful environments, providers can be served from a filesystem mirror or an internal network mirror instead of the public registry — terraform providers mirror builds the directory. Most teams don't need it; knowing it exists beats discovering you needed it during an incident.

Field notes

  • Pin required_version too — HCL syntax and core behavior shift between Terraform minors.
  • .terraform/ goes in .gitignore; .terraform.lock.hcl goes in git. Don't confuse them.
  • OpenTofu consumes the same registry protocol — source addresses work identically across both tools.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.