Extract configurations into modules, use the Terraform Registry, and structure a multi-environment repo.
infrastructure/
├── main.tf
├── variables.tf
├── outputs.tf
└── modules/
└── web-server/
├── main.tf ← module resources
├── variables.tf ← module inputs
└── outputs.tf ← module outputsvariable "instance_type" {
type = string
default = "t3.micro"
}
variable "name" {
type = string
}
variable "subnet_id" {
type = string
}
variable "security_group_ids" {
type = list(string)
}module "web" {
source = "./modules/web-server"
name = "production-web"
instance_type = "t3.small"
subnet_id = aws_subnet.public.id
security_group_ids = [aws_security_group.web.id]
}
# Public registry modules
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.1.0"
name = "my-vpc"
cidr = "10.0.0.0/16"
azs = ["us-east-1a", "us-east-1b"]
}terraform-aws-modules/vpc/aws is better than writing your own VPC from scratch — it handles edge cases you haven't thought of yet.module block and pass variables.module.name.output.version = "5.1.0".