Terraform Provider Configuration Patterns#
Providers are Terraform’s interface to cloud APIs. Misconfiguring them causes resources to be created in the wrong region, with the wrong credentials, or with an incompatible provider version. These failures are often silent — Terraform succeeds, but the resource is in the wrong place.
Version Constraints#
The Required Providers Block#
Every configuration should declare its providers with version constraints:
terraform {
required_version = ">= 1.5.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
kubernetes = {
source = "hashicorp/kubernetes"
version = "~> 2.25"
}
helm = {
source = "hashicorp/helm"
version = "~> 2.12"
}
}
}Version Constraint Syntax#
| Constraint | Meaning | Example |
|---|---|---|
= 5.31.0 | Exact version only | Pin for maximum reproducibility |
~> 5.0 | Any 5.x (>=5.0.0, <6.0.0) | Allow minor + patch updates |
~> 5.31 | Any 5.31.x (>=5.31.0, <5.32.0) | Allow patch updates only |
>= 5.0, < 6.0 | Range | Same as ~> 5.0 but explicit |
>= 5.0 | Any version 5.0 or newer | Dangerous — allows breaking changes |
Recommended patterns: