Terraform Modular Project Structure
Reusable modules, remote state, team-ready. Designed for large infrastructure and multiple projects.
Project Directory
terraform/
modules/
Reusable infrastructure modules
networking/
vpc/
main.tf
variables.tf
outputs.tf
versions.tf
README.md
security-groups/
main.tf
variables.tf
outputs.tf
compute/
ec2/
main.tf
variables.tf
outputs.tf
ecs/
main.tf
variables.tf
outputs.tf
data/
rds/
main.tf
variables.tf
outputs.tf
s3/
main.tf
variables.tf
outputs.tf
live/
Actual deployments
dev/
us-east-1/
Region-specific
networking/
terragrunt.hcl
services/
api/
terragrunt.hcl
env.hcl
Environment vars
prod/
us-east-1/
networking/
services/
eu-west-1/
Multi-region
networking/
services/
env.hcl
terragrunt.hcl
Root config
global/
Account-wide resources
iam/
dns/
s3-state/
State bucket bootstrap
Why This Structure?
Enterprise-grade structure using Terragrunt for DRY configurations. modules/ contains versioned, tested modules. live/ deploys those modules per environment and region. This scales to hundreds of resources across multiple accounts.
Key Directories
- modules/-Reusable modules, versioned separately
- live/-Actual deployments organized by env/region
- live/*/env.hcl-Environment-specific variables
- global/-Account-wide resources like IAM, DNS
- terragrunt.hcl-DRY configuration with Terragrunt
Terragrunt Config
# live/prod/us-east-1/services/api/terragrunt.hcl
include "root" {
path = find_in_parent_folders()
}
terraform {
source = "../../../../../modules/compute/ecs"
}
inputs = {
cluster_name = "api-prod"
instance_count = 3
instance_type = "t3.large"
}
Getting Started
- Install Terraform and Terragrunt
- Bootstrap state bucket in
global/s3-state/ - Create first module in
modules/ - Deploy via
live/dev/with Terragrunt cd live/dev/us-east-1/networking && terragrunt apply
When To Use This
- Large infrastructure (100+ resources)
- Multiple AWS accounts and regions
- Infrastructure team with many members
- Need module versioning and testing
- Compliance requires audit trails
Trade-offs
- Complexity-Steeper learning curve, more tooling
- Terragrunt dependency-Additional tool to maintain
- Initial setup-More upfront work before first deploy
Testing Strategy
- Module tests-Use Terratest or tftest for module validation
- Plan review-Always
planbeforeapplyin CI - Policy as code-OPA or Sentinel for compliance checks
- Drift detection-Scheduled plans to detect manual changes