name: design-terraform-project-structure description: Use when starting infrastructure-as-code with Terraform or OpenTofu, or when an existing setup has one giant state file, copy-pasted environment folders, modules that do everything, slow plans, or changes to dev that risk prod — and you need a module, environment, and state layout. source: 'HashiCorp, "Standard Module Structure" (Terraform documentation); HashiCorp, "Terraform Recommended Practices"; Google Cloud, "Best practices for general style and structure" (Terraform on Google Cloud); AWS Prescriptive Guidance, "Terraform AWS Provider Best Practices"; Brikman, "Terraform: Up & Running", 3rd ed., 2022' tags: [project-structure, terraform, infrastructure-as-code, modules, state-management, platform-engineering] related: [apply-iac-security, audit-infra-drift, design-gitops-workflow, design-ci-pipeline, design-repository-layout]
Design Terraform Project Structure
Separate reusable modules (how to build things) from root configurations (what to deploy where), split state by environment and component to limit blast radius, and keep environments identical except for their inputs.
Why This Is Best Practice
Adopted by:
- HashiCorp's own Standard Module Structure and recommended practices.
- Google Cloud's and AWS's official Terraform best-practice guides.
- Gruntwork's (Brikman's) reference architecture.
- Platform teams generally, which publish internal module registries.
Impact:
- Splitting state into smaller root modules shortens plan/apply time and limits the blast radius of a bad change or a corrupted state file to one component in one environment (HashiCorp, Google Cloud guides).
- Versioned modules let many teams reuse vetted, secure building blocks instead of copy-pasting resources.
Why best:
- A single monolithic configuration means every change plans the whole estate and one mistake can touch production.
- Copy-pasted environment folders drift apart.
- Module/root separation plus per-environment state gives reuse, isolation, and environments that differ only in documented variables.
Sources: HashiCorp, "Standard Module Structure" and "Recommended Practices" (developer.hashicorp.com). Google Cloud, "Best practices for general style and structure". AWS Prescriptive Guidance, Terraform best practices. Brikman, Y. (2022), Terraform: Up & Running, 3rd ed., O'Reilly.
Steps
Use the standard layout:
modules/ # reusable, versioned building blocks network/ main.tf variables.tf outputs.tf versions.tf README.md examples/basic/ tests/ # terraform test / Terratest postgres/ live/ (or envs/, stacks/) # root modules = deployable units, one state each dev/ network/ main.tf backend.tf terraform.tfvars app/ staging/ prod/ network/ app/Modules can also live in separate repos or a private registry, consumed by version.
Follow the Standard Module Structure in every module:
main.tf,variables.tf(typed, described, validated),outputs.tf,versions.tf(pinnedrequired_providersandrequired_version), aREADME.mdgenerated with terraform-docs, andexamples/.Design modules around one purpose (network, database, service). Avoid thin wrappers around a single resource, and avoid "everything" modules. Expose only the inputs that genuinely vary. Give the rest secure defaults.
Split state by environment × component. Each root module has its own remote backend key (S3 + DynamoDB/S3 native locking, GCS, Terraform Cloud/HCP). Separate slowly changing foundations (network, IAM) from frequently changing apps. Pass values between stacks through outputs or data sources, not by sharing state.
Keep environments identical except for inputs. Each environment's root calls the same module versions with different
tfvars. Remove remaining duplication with Terragrunt or Terraform Stacks if needed. Avoid relying on CLI workspaces for strongly isolated environments like prod. Separate accounts or projects per environment are safer.Pin and promote module versions. Reference modules by tag or registry version (
?ref=v1.4.0). Upgrade dev first, then staging, then prod.Make CI the only path to apply. Format, validate, lint (tflint), run security scans (Checkov/Trivy, see
apply-iac-security), andplanon the pull request, thenapplyon merge with reviewed plans. Detect drift on a schedule (seeaudit-infra-drift).Use consistent conventions.
- snake_case resource names
- a
locals.tffor computed values - common
tags/labelsapplied everywhere - one
providers.tfper root - no hard-coded account IDs or regions in modules
Rules
- Modules are reusable and versioned. Roots are deployable and hold state.
- One state per environment per component. Prod never shares state with non-prod.
- Environments differ only in variables, never in copy-pasted code.
- Pin Terraform, provider, and module versions.
- Humans don't run
applyagainst prod from laptops.
Common Mistakes
- One state for everything. Slow plans, lock contention, and a huge blast radius.
- Copy-pasted
dev/,prod/code that drifts. Use shared modules plus tfvars. - Modules with 80 variables that mirror every resource argument. That's no abstraction at all.
- Unpinned module sources (
ref=main). Upstream changes silently alter prod. - Secrets in
tfvarsor state left unencrypted. Use a secret manager, an encrypted backend, and restricted state access.
Examples
AWS platform:
modules/vpc,modules/ecs-service, andmodules/rdsare published with semver tags.live/prod/vpcandlive/prod/orders-serviceeach have their own S3 state key.- A pull request triggers
planvia Atlantis or GitHub Actions. Merge triggersapply.
Multi-team setup: the platform team owns the modules/ registry and foundation stacks. Product teams own live/<env>/<service> roots that consume pinned module versions.
When NOT to Use
- A single small personal project where one root and local state are acceptable. Still use a remote backend before collaborating.
- Organizations standardized on a different tool (Pulumi, CDK, Crossplane). Apply the same principles (reusable components, isolated stacks per environment) in that tool's idioms.