Terraform · Lesson 3 of 12 · 3 min read
Providers and version management
Declaring and pinning
Enlarge diagram
Swipe sideways to see all of it, or tap to open it full screen.
terraform {
required_version = ">= 1.11, < 2.0" # 1.11+ for S3-native state locking
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0" # >= 6.0, < 7.0
}
kubernetes = { source = "hashicorp/kubernetes", version = "~> 2.33" }
helm = { source = "hashicorp/helm", version = "~> 2.16" }
}
}
Constraint operators: =, !=, >, >=, <, <=, and ~> (pessimistic: ~> 6.0 allows any 6.x; ~> 6.2.1 allows 6.2.x from 6.2.1 only).
The .terraform.lock.hcl file records the exact provider versions and checksums selected at init. Commit it — everyone and CI then use identical providers. Upgrade deliberately with terraform init -upgrade and review the diff (major provider versions can change resource behaviour).
Configuring the AWS provider
provider "aws" {
region = "ap-south-1"
default_tags {
tags = local.common_tags # applied to every taggable resource
}
}
provider "aws" {
alias = "us_east_1" # second configuration: e.g. ACM certs for CloudFront
region = "us-east-1"
}
provider "aws" {
alias = "shared_services"
region = "ap-south-1"
assume_role { # manage resources in another account
role_arn = "arn:aws:iam::222222222222:role/terraform-deployer"
}
}
resource "aws_acm_certificate" "cdn" {
provider = aws.us_east_1
domain_name = "cdn.example.com"
validation_method = "DNS"
}
Credentials come from the standard AWS chain — environment variables, ~/.aws profiles, SSO, instance/pod roles, or OIDC in CI — never hardcoded in .tf files.