A Terraform working directory does almost nothing on its own. The Terraform binary itself only understands HCL syntax, the dependency graph, and the planning/apply loop. Every concrete resource type, every data source, every authentication method against a real API lives inside a provider plugin that Terraform must download before any meaningful operation can happen. Provider installation is therefore not a side task; it is the gate that turns a .tf file from a text document into something terraform plan can reason about. When a practitioner runs terraform init for the first time, the bulk of the wall-clock time is spent resolving provider source addresses, checking version constraints, downloading the platform-specific binary, and writing a dependency lock file that records exactly what was selected.
The declaration that drives that resolution lives inside the top-level terraform block. Specifically, the required_providers map associates a local name (the key Terraform uses to refer to the provider inside this configuration) with a source address and an optional version constraint. The local name is just a handle; the source is what actually identifies the plugin against a registry. A source address is a three-part value: hostname/namespace/type. When the hostname is omitted, Terraform implicitly assumes registry.terraform.io, the public Terraform Registry. The namespace is the publishing organization (for HashiCorp-maintained providers, that is hashicorp), and the type is the provider's short name (random, null, aws, azurerm, google, and so on). Two configurations can use the same local name to refer to completely different providers as long as their source addresses differ, which is how you wire forks, mirrors, or private registries into a project without rewriting downstream resource blocks.
Version constraints belong on the version argument inside required_providers. Terraform interprets the constraint string with a small grammar: = for an exact version, != to forbid one, >=, <=, >, < for open or half-open ranges, and the pessimistic constraint operator ~> which allows the rightmost release component to increase but locks every component to its left. A constraint like ~> 5.30 permits any version from 5.30.0 up to but excluding 6.0.0, while ~> 5.30.0 permits 5.30.0 up to but excluding 5.31.0. Multiple constraints can be combined with commas, so >= 5.20, < 6.0, != 5.25.0 is a legal expression that excludes a known-bad release inside a wider range. Terraform picks the highest version that satisfies the union of every constraint that mentions that provider across the root module and every module it includes.
The dependency lock file (.terraform.lock.hcl) is the bridge between flexible constraints in source code and reproducible builds in operations. When terraform init resolves provider versions, it writes the exact version it chose and the cryptographic hashes of each platform binary it has seen into the lock file at the root of the working directory. Subsequent terraform init runs on the same machine, on a teammate's machine, or in CI will refuse to deviate from those locked versions unless you explicitly request an upgrade with terraform init -upgrade. The lock file should be checked into version control. Treating it as a build artifact, regenerating it on every CI run, defeats its purpose: the entire point is that two engineers running the same configuration get the same provider binaries.
Declaring required providers and the lock file
The minimum complete declaration looks like the block below. It pins Terraform itself to a supported release, lists two providers from the public registry, applies version constraints, and registers a local alias name (rand) that differs from the provider's type.
terraform {
required_version = ">= 1.12.0, < 2.0.0"
required_providers {
rand = {
source = "hashicorp/random"
version = "~> 3.6"
}
null = {
source = "hashicorp/null"
version = ">= 3.2, < 4.0"
}
}
}
provider "rand" {}
provider "null" {}
resource "random_pet" "demo" {
length = 3
}
Notice how the resource block uses the canonical type name random_pet. Even though the local name inside required_providers is rand, the resource type prefix is bound to the provider through the source address, not the local handle. The local handle matters for provider configuration blocks and for the resource-level provider meta-argument; the type prefix on resource and data blocks is fixed by whoever published the provider.
After writing this configuration, the practitioner runs:
terraform init
Terraform reads required_providers, contacts the registry, downloads the binaries that match the resolved versions for the current operating system and architecture, places them under .terraform/providers/, and writes selections plus hashes into .terraform.lock.hcl. To validate that the lock file already covers a CI runner with a different OS or CPU, run terraform providers lock -platform=linux_amd64 -platform=darwin_arm64. The command extends the lock file with hashes for additional platforms without changing the selected versions, so a workstation lock can travel safely to a Linux container.
💡 Exam Trap: The pessimistic operator
~>does not mean "the highest minor version below the next major version" in every shape.~> 3permits anything>= 3.0, < 4.0.~> 3.6permits>= 3.6, < 4.0.~> 3.6.0permits>= 3.6.0, < 3.7.0. The position of the rightmost component is what the operator pins.
💡 Exam Trap: Local names inside
required_providersare not arbitrary aliases for the provider's type prefix. They control howproviderblocks are addressed and how theprovider = local_name.aliasmeta-argument resolves, but they cannot renamerandom_pettorand_peton aresourceblock. Resource type names are always the canonical names from the provider's schema.
Version constraints and resolution
Constraints are unioned across every module that names the provider, and Terraform picks the highest version that simultaneously satisfies every constraint. Two modules with incompatible windows cause an init-time failure rather than a silent downgrade. A common mistake in larger code bases is publishing a child module without a required_providers block at all; Terraform then infers no constraint from that module, but it also cannot guarantee the child module will work with whatever version the root resolves. The exam-worthy rule is: every module that uses a provider should declare it in its own required_providers, even when the version constraint is loose, so the resolver has accurate data.
# child module: modules/network/versions.tf
terraform {
required_version = ">= 1.12.0"
required_providers {
examplecloud = {
source = "examplecorp/examplecloud"
version = ">= 2.4, < 3.0"
}
}
}
When the root module pulls this child module in and itself declares version = "~> 2.7", the resolver intersects >= 2.4, < 3.0 with ~> 2.7 and picks the highest release in the intersection, namely the latest 2.7.x or later patch up to but excluding 2.8.0 if the root constraint had been ~> 2.7.0, or the latest 2.x patch up to but excluding 3.0 if the root constraint had been ~> 2.7.
The dependency lock file then records the exact version selected. If a teammate clones the repository, runs terraform init without flags, and the lock file pins 2.7.4, they get 2.7.4 regardless of whether 2.7.5 has since been released. To accept an upgrade inside the constraint window:
terraform init -upgrade
This re-evaluates every provider against its constraints, picks the highest satisfying release, downloads it, and rewrites the lock file. The -upgrade flag is the only way to move the lock forward; plain terraform init will not.
📊 Limit: Terraform Core itself enforces
required_versionin theterraformblock. If the installed CLI does not match,init,plan, andapplyall fail before any provider work happens. Use>= 1.12.0, < 2.0.0instead of= 1.12.4so you do not block on patch releases.
📊 Limit: The dependency lock file records SHA-256 hashes for every platform Terraform has observed. If you run on a platform Terraform has not seen, init fails with a missing-hash error unless you extend the lock with
terraform providers lock -platform=....
Plugin cache, mirrors, and offline installation
Three mechanisms accelerate or constrain provider downloads in environments where pulling from the public registry on every init is undesirable. The first is the plugin cache: setting the TF_PLUGIN_CACHE_DIR environment variable (or plugin_cache_dir in the CLI configuration file) points Terraform at a shared directory it uses to deduplicate downloads across working directories. The second is a provider installation mirror: a filesystem or network location populated with provider archives that Terraform should consult before contacting any registry. The third is a private registry, which speaks the registry protocol and lets organizations publish internal forks alongside their own providers.
# CLI config: ~/.terraformrc on Linux/macOS, %APPDATA%/terraform.rc on Windows
plugin_cache_dir = "$HOME/.terraform.d/plugin-cache"
provider_installation {
filesystem_mirror {
path = "/opt/terraform/providers"
include = ["examplecorp/*"]
}
direct {
exclude = ["examplecorp/*"]
}
}
This config tells Terraform: for any provider in the examplecorp namespace, install from the local mirror; for everything else, talk to the registry. The cache directory persists binaries between init runs so that creating a new working directory does not re-download every plugin from scratch.
⚠️ Anti-Pattern: Adding
.terraform.lock.hclto.gitignore. Without the lock file in version control, every collaborator and every CI run starts from "highest version that satisfies the constraint," meaning upgrades sneak in silently. The lock file is part of the source, not a build artifact.
🎯 Scenario: A platform team ships a new release of an internal provider under the source
examplecorp/internal. Engineers report thatterraform planon existing stacks suddenly emits diffs that none of them authored. Investigation shows the stacks had no version constraint inrequired_providersand no lock file in the repo. With everyinit, the registry returned the newest provider version, whose schema changed an attribute's default. The fix: pin a~> 1.4constraint, runterraform init -upgrade, commit the resulting.terraform.lock.hcl, and add CI verification thatterraform initproduces no lock-file diff.
Decision Anchor
Choose explicit ~> constraints with a checked-in lock file when you operate the configuration in production or share it with collaborators, since you need reproducibility plus a controlled upgrade path. Choose looser >= constraints, still backed by the lock file, only for short-lived experiments where you accept that upgrades inside the same major version may land on the next init -upgrade. Avoid constraints with no upper bound on any long-lived configuration: a major-version bump can rewrite resource schemas and break the apply.
