Skip to main content

Code Organization

Repository Layout

Use a mono-repo structure with a clear separation between live infrastructure and reusable modules:

infrastructure/
├── environments/
│ ├── dev/
│ │ ├── main.tf
│ │ ├── variables.tf
│ │ ├── outputs.tf
│ │ └── terraform.tfvars
│ ├── staging/
│ └── prod/
└── modules/
├── networking/
├── rds_cluster/
└── ecs_service/
  • Each environment directory is an independent root module with its own state file.
  • Shared logic lives exclusively in modules/. Copy-paste between environments is a red flag.

File Layout Within a Module

Every Terraform root module and reusable module must follow this file layout:

FilePurpose
main.tfCore resource declarations
variables.tfAll variable blocks with descriptions and types
outputs.tfAll output blocks
versions.tfterraform block with required_version and required_providers
locals.tfAll local blocks (omit if empty)
data.tfAll data source blocks (omit if empty)

Do not create files named resources.tf or split resources arbitrarily across files. Group by logical concern when a module is large (e.g. iam.tf, security_groups.tf).