Providers, Versions & the Lock File
A provider is a separate program that the CLI starts and talks to over a local plugin protocol. Your code says which providers it needs and which versions are acceptable; init picks concrete versions and records them in the lock file; provider blocks configure them.
required_providers
terraform {
required_version = ">= 1.6" # the CLI itself
required_providers {
aws = {
source = "hashicorp/aws" # <namespace>/<type> on the registry
version = "~> 6.0" # any 6.x, never 7.0
}
}
}The source address is [hostname/]namespace/type. Without a hostname it means the default registry: registry.terraform.io for Terraform, registry.opentofu.org for OpenTofu. Both registries serve the same HashiCorp-published provider releases, so hashicorp/aws works with either CLI. The local name on the left (aws) is what resource types start with (aws_vpc).
Version constraints:
| Constraint | Means |
|---|---|
= 6.70.0 or 6.70.0 |
exactly that version |
>= 6.0, < 7.0 |
a range (comma = and) |
~> 6.0 |
>= 6.0, < 7.0: only the rightmost given part may grow |
~> 6.70.0 |
>= 6.70.0, < 6.71.0: patch releases only |
!= 6.12.0 |
exclude a known-bad release |
Rule of thumb: reusable modules state the widest range they work with (>= 5.0); root configurations use ~> and rely on the lock file for the exact version.
The lock file
init resolves the constraints to concrete versions and writes .terraform.lock.hcl:
mkdir p && cd p
cat > versions.tf <<'HCL'
terraform {
required_providers {
null = { source = "hashicorp/null", version = ">= 3.2, < 4.0" }
tls = { source = "hashicorp/tls", version = "~> 4.0" }
}
}
HCL
tofu init >/dev/null
cat .terraform.lock.hcl
find .terraform -type f | sort# This file is maintained automatically by "tofu init".
# Manual edits may be lost in future updates.
provider "registry.opentofu.org/hashicorp/null" {
version = "3.3.2"
constraints = ">= 3.2.0, < 4.0.0"
hashes = [
"h1:IQ1qrkht1sC1nibUR+AJ3ulryyhVDHfCHZhoJi0sg2Y=",
]
}
provider "registry.opentofu.org/hashicorp/tls" {
version = "4.4.2"
constraints = "~> 4.0"
hashes = [
"h1:cMw+jmw1zbZ2Tgl2zwm1pU3S5jySk9i2W3ZRzu7YaVI=",
]
}
.terraform/providers/registry.opentofu.org/hashicorp/null/3.3.2/linux_amd64.lock
.terraform/providers/registry.opentofu.org/hashicorp/null/3.3.2/linux_amd64/LICENSE.txt
.terraform/providers/registry.opentofu.org/hashicorp/null/3.3.2/linux_amd64/terraform-provider-null_v3.3.2_x5
.terraform/providers/registry.opentofu.org/hashicorp/tls/4.4.2/linux_amd64.lock
.terraform/providers/registry.opentofu.org/hashicorp/tls/4.4.2/linux_amd64/LICENSE.txt
.terraform/providers/registry.opentofu.org/hashicorp/tls/4.4.2/linux_amd64/terraform-provider-tls_v4.4.2_x5
- The lock file stores the selected version, the constraints it satisfied, and hashes of the provider package. On the next
init, anyone on the team (and CI) gets exactly 3.3.2 and the download is checked against the hash: a tampered or swapped binary fails. - OpenTofu writes
registry.opentofu.org/…addresses, Terraformregistry.terraform.io/…. A repository switching CLIs regenerates the lock file. - Only
init -upgrademoves to a newer version within the constraints. That is a deliberate change: run it, review the lock-file diff in a pull request, merge. tofu providers(used in the session below) prints which module requires what, which matters once modules bring their own constraints.
Hashes for every platform
Developers on macOS (darwin_arm64) and CI on Linux (linux_amd64) need hashes for both. When init installs from a mirror, as in this sandbox, it can only hash the one platform it downloaded, hence the "Incomplete lock file information" warning you saw in Install & Your First Apply. Installing from the registry, OpenTofu 1.12+ records the checksums of every platform during init by itself; with Terraform, or after installing from a mirror, run providers lock. It needs the registry, so it is a sample session:
$ tofu providers lock -platform=linux_amd64 -platform=darwin_arm64 -platform=windows_amd64
- Fetching hashicorp/null 3.3.2 for linux_amd64...
- Retrieved hashicorp/null 3.3.2 for linux_amd64 (signed, key ID 0C0AF313E5FD9F80)
- Obtained hashicorp/null checksums for linux_amd64; All checksums for this platform were already tracked in the lock file
- Fetching hashicorp/null 3.3.2 for darwin_arm64...
…
Success! OpenTofu has updated the lock file.When no version matches
Constraints are checked against what the registry (here: the mirror) offers. The sandbox only has aws 6.70.0, so asking for 5.x fails, exactly as asking for a version that was never released would:
mkdir q && cd q
cat > main.tf <<'HCL'
terraform {
required_providers {
aws = { source = "hashicorp/aws", version = "~> 5.0" }
}
}
HCL
tofu init; echo "exit=$?"
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "~> 5.0"...
Error: Failed to resolve provider packages
Could not resolve provider hashicorp/aws: no available releases match the
given constraints ~> 5.0
exit=1
Configuring a provider, and aliases
A provider block configures the plugin. Credentials don't belong in it (the AWS provider lesson covers where they come from); settings such as region and default tags do. To use the same provider twice, say for a second region, give the extra configuration an alias and select it per resource with the provider meta-argument:
mkdir regions && cd regions
cat > main.tf <<'HCL'
terraform {
required_providers {
aws = { source = "hashicorp/aws", version = "~> 6.0" }
}
}
provider "aws" {
region = "eu-west-1"
}
provider "aws" {
alias = "us"
region = "us-east-1" # e.g. ACM certificates for CloudFront must live here
}
resource "aws_s3_bucket" "logs" {
bucket = "linkly-logs-eu"
}
resource "aws_s3_bucket" "logs_us" {
provider = aws.us
bucket = "linkly-logs-us"
}
HCL
tofu init >/dev/null
tofu validate
tofu providersSuccess! The configuration is valid.
Providers required by configuration:
.
└── provider[registry.opentofu.org/hashicorp/aws] ~> 6.0
The tree lists hashicorp/aws once: the two provider blocks are two configurations of one plugin, and init installs it once. A resource without provider = uses the default (unaliased) configuration. Modules receive aliased providers through providers = { … } in the module block (Modules).
Note: AWS provider 6 also accepts a
regionargument on most resources, so one provider configuration can create resources in several regions. Aliases are still needed when other settings differ (another account throughassume_role, other credentials).
Test yourself
What does version = "~> 6.0" allow?
- Exactly 6.0
- 6.0 and every later version
- Any 6.x release, but not 7.0
- Only 6.0.x patch releases
Your teammate's init picks a newer provider than yours although the code is the same. What was missing?
- A
providerblock - The committed
.terraform.lock.hcl - The
.terraform/folder in Git required_version
How do you create an S3 bucket with a second configuration of the AWS provider that has alias = "us"?
provider = aws.usinside the resourcealias = "us"inside the resource- Name the resource
aws_us_s3_bucket - Run
tofu apply -provider=us