# Design Terraform Project Structure

> Use when starting infrastructure-as-code with Terraform or OpenTofu, or when an existing setup has one giant state file, copy-pasted environment folders, module

- Skill: `jeffreytse/design-terraform-project-structure` (Agent Skill)
- Install (CLI): `npx get-skill add jeffreytse/design-terraform-project-structure`
- Raw SKILL.md: https://api.skillmd.com/api/skills/jeffreytse/design-terraform-project-structure/raw
- Safety review: pending
- Works with: any agent that reads SKILL.md (Claude Code, Claude.ai, Cursor, Codex, Windsurf, 60+ more)
- Category: Coding & Dev Tools
- Author: jeffreytse (https://skillmd.com/u/jeffreytse)
- Updated: 2026-12-03
- Page: https://skillmd.com/skills/jeffreytse/design-terraform-project-structure

---

---
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

1. **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.

2. **Follow the Standard Module Structure** in every module: `main.tf`, `variables.tf` (typed, described, validated), `outputs.tf`, `versions.tf` (pinned `required_providers` and `required_version`), a `README.md` generated with terraform-docs, and `examples/`.

3. **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.

4. **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.

5. **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.

6. **Pin and promote module versions.** Reference modules by tag or registry version (`?ref=v1.4.0`). Upgrade dev first, then staging, then prod.

7. **Make CI the only path to apply.** Format, validate, lint (tflint), run security scans (Checkov/Trivy, see `apply-iac-security`), and `plan` on the pull request, then `apply` on merge with reviewed plans. Detect drift on a schedule (see `audit-infra-drift`).

8. **Use consistent conventions.**
   - snake_case resource names
   - a `locals.tf` for computed values
   - common `tags`/`labels` applied everywhere
   - one `providers.tf` per 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 `apply` against 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 `tfvars` or state left unencrypted.** Use a secret manager, an encrypted backend, and restricted state access.

## Examples

**AWS platform:**
- `modules/vpc`, `modules/ecs-service`, and `modules/rds` are published with semver tags.
- `live/prod/vpc` and `live/prod/orders-service` each have their own S3 state key.
- A pull request triggers `plan` via Atlantis or GitHub Actions. Merge triggers `apply`.

**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.

