Show me the best examples of Teraform files that are using AWS provider with different…
- User
Show me the best examples of Teraform files that are using AWS provider with different environments and parametrized as possible
- Assistant
Show me the best examples of Teraform files that are using AWS provider with different environments and parametrized as possible
Below is a mini-reference project that puts all the “greatest-hit” Terraform tricks in one place:
• clean separation between reusable modules and “live” environments
• fully parameterised AWS provider (region, profile, tags, etc.)
• one source of truth for defaults, but easy overrides per-env
• remote state, state locking & drift protection
• works with either workspaces or directory-per-env workflows
Copy/paste the bits you like – or just fork the whole structure.
terraform-aws-example
├── modules/ # Re-usable lego bricks
│ ├── network/ # VPC, subnets…
│ └── eks/ # Managed EKS cluster
└── live/ # “Real” environments
├── dev/
├── stage/
└── prod/
modules/network)modules/network/variables.tf
variable "cidr" { type = string }
variable "az_count" { type = number default = 3 }
variable "public_subnets" { type = list(any) default = [] }
variable "private_subnets" { type = list(any) default = [] }
variable "tags" { type = map(string) default = {} }
modules/network/main.tf
resource "aws_vpc" "this" {
cidr_block = var.cidr
tags = merge(var.tags, { Name = "${var.tags["Project"]}-vpc" })
}
resource "aws_subnet" "public" {
count = var.az_count
vpc_id = aws_vpc.this.id
cidr_block = var.public_subnets[count.index]
availability_zone = data.aws_availability_zones.available.names[count.index]
map_public_ip_on_launch = true
tags = merge(var.tags, { "Tier" = "public" })
}
resource "aws_subnet" "private" {
count = var.az_count
vpc_id = aws_vpc.this.id
cidr_block = var.private_subnets[count.index]
availability_zone = data.aws_availability_zones.available.names[count.index]
tags = merge(var.tags, { "Tier" = "private" })
}
output "vpc_id" { value = aws_vpc.this.id }
output "public_subnets_ids" { value = aws_subnet.public[*].id }
output "private_subnets_ids" { value = aws_subnet.private[*].id }
live/_common.tf)terraform {
required_version = ">= 1.4"
required_providers {
aws = { source = "hashicorp/aws", version = "~> 5.0" }
}
# Remote state; bucket & key built dynamically (see below)
backend "s3" {}
}
############################
# Globals, no hard-coding! #
############################
locals {
# Map workspace → AWS region
region_map = {
dev = "us-west-2"
stage = "us-east-2"
prod = "us-east-1"
}
# Map workspace → AWS CLI profile (optional)
profile_map = {
dev = "dev"
stage = "stage"
prod = "prod"
}
# Universal tags
default_tags = {
Project = "my-saas"
ManagedBy = "terraform"
Environment = terraform.workspace
}
}
provider "aws" {
region = lookup(local.region_map, terraform.workspace, "us-east-1")
profile = lookup(local.profile_map, terraform.workspace, null)
default_tags { tags = local.default_tags }
}
######################
# Remote state block #
######################
terraform {
backend "s3" {
# These three *must* be fixed strings.
bucket = "mycompany-terraform-state"
region = "us-east-1"
dynamodb_table = "terraform-locks"
encrypt = true
# key cannot use interpolation inside the .tf file,
# so we’ll override at init time (see Makefile below).
key = "placeholder.tfstate"
}
}
Why the “placeholder”? Because interpolation like ${terraform.workspace} is forbidden inside backend blocks. A common workaround is:
terraform -chdir=live/dev init \
-backend-config="key=aws/dev/terraform.tfstate"
or script it in a Makefile.
live/dev)live/dev/versions.auto.tfvars
# Only values that *differ* from global defaults
cidr_block = "10.10.0.0/16" # dev VPC
az_count = 2
public_subnets = ["10.10.1.0/24", "10.10.2.0/24"]
private_subnets = ["10.10.11.0/24", "10.10.12.0/24"]
extra_tags = { "Team" = "Platform" }
live/dev/main.tf
# Pull in shared logic
module "network" {
source = "../../modules/network"
cidr = var.cidr_block
az_count = var.az_count
public_subnets = var.public_subnets
private_subnets = var.private_subnets
# Merge default/global tags + any environment-only extra_tags
tags = merge(local.default_tags, var.extra_tags)
}
live/dev/variables.tf
variable "cidr_block" { type = string }
variable "az_count" { type = number }
variable "public_subnets" { type = list(string) }
variable "private_subnets" { type = list(string) }
variable "extra_tags" { type = map(string) default = {} }
stage & prodJust duplicate live/dev into stage and prod, tweak the *.tfvars.
Nothing in the modules changes.
If you prefer one directory + terraform workspace new dev instead of live/dev, delete the three sub-dirs and keep a single live/ folder.
Replace all explicit var.* lookups with maps keyed by terraform.workspace – the rest of the code stays identical.
Makefile (partial)
ENV ?= dev # default env
init:
terraform -chdir=live/$(ENV) init \
-backend-config="key=aws/$(ENV)/terraform.tfstate"
plan:
terraform -chdir=live/$(ENV) plan -var-file="versions.auto.tfvars"
apply:
terraform -chdir=live/$(ENV) apply -var-file="versions.auto.tfvars"
.PHONY: init plan apply
• Store AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN in your CI secrets manager.
• Add terraform fmt -check, terraform validate & tflint to PR pipeline.
• For prod, wrap terraform apply behind manual approval in your CI (GitHub Actions “environment”, GitLab protected stage, etc.).
• Consider Sentinel or OPA/Conftest for policy-as-code (“no public S3 in prod”, “EKS must use CMK encryption” …).
modules/.live/<env>/.terraform.workspace.That’s the pattern used in most mature Terraform+AWS codebases today.✌️