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:
initon another machine verifies the downloaded binary against the recorded hashes instead of trusting the registry fresh.initwithout-upgraderefuses to change provider versions — reproducible installs, the waypackage-lock.jsonmakesnpm cireproducible.- A
git diffof 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_arm64init 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_versiontoo — HCL syntax and core behavior shift between Terraform minors. .terraform/goes in.gitignore;.terraform.lock.hclgoes in git. Don't confuse them.- OpenTofu consumes the same registry protocol —
sourceaddresses work identically across both tools.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
AWS Emulator Puts AWS on Your Laptop. Keep Your IAM Tests Off It.
AWS released AWS Emulator on November 11, a free local server that answers AWS API calls on 127.0.0.1 with per-run keys and fresh certificates.
What is Infrastructure as a Service? The Layer That Makes Everything Else Possible
Infrastructure as a Service gives you virtualized servers, storage, and networking on demand without owning any hardware. You rent compute and own everything that runs on it. Here is what IaaS actually is, what it demands from you.
What is Platform as a Service? The Sweet Spot Between "Too Easy" and "Too Much Work"
Platform as a Service sits between raw servers and fully managed software. You push code, the platform runs it. No Nginx config, no OS patches, no 3 AM server alerts. Here is what PaaS actually gives you, what it costs you in control, and how to decide if it is the right layer for your application.
The World now is Serverless and Container based
A new report from Datadog reveals that serverless computing could be entering the mainstream. This has already been the case in my world for the past few years. Results show that more than half of all organizations are using serverless on one of the three major public cloud providers: Amazon,