A reliable Terraform project structure makes infrastructure easier to change, review, and operate across preproduction and production. This guide provides a practical layout for modules, environments, variables, and remote state, along with a reusable checklist for teams working through infrastructure changes.
Overview
Terraform projects become difficult to maintain when environment-specific settings, reusable infrastructure, state files, and deployment logic are mixed together. A clear structure separates those concerns without hiding how resources are assembled.
There is no single repository layout that fits every organization. A small team managing one cloud account may need only a few root configurations. A platform team supporting several application teams may need separate environment directories, versioned modules, and stronger controls around state and approvals. The right structure is the one that makes ownership, impact, and promotion paths easy to understand.
A useful design usually has four layers:
- Root modules: The executable Terraform configurations used for a particular environment or deployment scope.
- Reusable modules: Composable building blocks for infrastructure patterns such as networks, databases, or Kubernetes clusters.
- Configuration: Variables, environment-specific values, provider settings, and approved defaults.
- State and workflow controls: Remote state storage, locking where supported, plan review, access controls, and CI/CD execution.
Keep the configuration declarative and make the workflow predictable: format and validate changes locally, generate a plan in CI, review the plan in the pull request, and apply only from an approved context. For a more detailed review process, see the Terraform plan review checklist.
Checklist by scenario
Starting a small Terraform project
For a small project, begin with one root configuration and avoid creating abstractions before a repeated pattern exists. A practical starting structure could look like this:
terraform-project/
├── main.tf
├── providers.tf
├── variables.tf
├── outputs.tf
├── versions.tf
├── terraform.tfvars.example
├── modules/
│ └── network/
│ ├── main.tf
│ ├── variables.tf
│ └── outputs.tf
└── README.md
Keep real secrets out of .tfvars files committed to the repository. Document required variables in the example file and supply sensitive values through the CI system, a secrets manager, or another controlled mechanism. Pin the Terraform and provider versions to an intentionally chosen range, then review those constraints as part of routine maintenance.
Managing multiple environments
When preproduction and production need different values or accounts, separate the root configurations so each environment has an explicit entry point. One common approach is:
terraform-project/
├── modules/
│ ├── network/
│ └── application/
└── environments/
├── preproduction/
│ ├── main.tf
│ ├── providers.tf
│ ├── variables.tf
│ └── outputs.tf
└── production/
├── main.tf
├── providers.tf
├── variables.tf
└── outputs.tf
This layout makes the environment boundary visible in commands, state configuration, and pull requests. It also supports different credentials, regions, accounts, or approval rules without relying entirely on a workspace name or an operator remembering extra flags.
Keep the resource composition similar where the environments are intended to represent the same system. Differences should be deliberate and documented, such as instance sizing, replica counts, retention settings, or domain names. If the environments have fundamentally different architecture, separate root modules may be clearer than forcing every difference through conditionals.
Building reusable modules
Create a module when a group of resources represents a meaningful capability or when the same pattern is used more than once. A module should have a narrow interface: required inputs, optional settings with sensible defaults, and outputs that callers actually need.
For example, a network module might accept a project or account identifier, address range, and subnet definitions. It can output network and subnet identifiers for other modules. Avoid exposing every provider argument directly unless callers genuinely need that level of control. Excessive inputs make modules harder to understand and increase the chance that environments drift.
Use consistent file conventions such as main.tf for resources, variables.tf for inputs, outputs.tf for outputs, and README.md for usage. Add examples or tests where the module has important edge cases. Treat module interfaces as contracts: changing a variable or output can affect every root configuration that calls it.
Working with multiple teams
For shared infrastructure, define ownership at the state boundary. A team should know which resources it can change, which state it owns, and which dependencies are managed elsewhere. Splitting state by environment, system, or lifecycle can reduce unrelated changes in the same plan, but too many small states can create dependency and coordination overhead.
Use explicit outputs and remote-state consumption only where a real dependency exists. If teams frequently need to read values from another state, consider whether the interface should be documented, published through a service configuration system, or managed by a shared module instead.
What to double-check
- State location: Confirm that each environment points to the intended remote state location and that state files are not being stored in the repository.
- Locking and concurrency: Verify that the selected backend supports the team’s approach to preventing overlapping changes, and ensure CI jobs do not apply the same state concurrently.
- Credentials: Check that Terraform runs with short-lived or appropriately scoped credentials where possible. Do not place credentials, tokens, or private keys in variables committed to source control.
- Provider and Terraform constraints: Review version constraints, lock files, provider aliases, and upgrade behavior together. A provider upgrade can change planning or resource behavior even when the configuration appears unchanged.
- Environment separation: Confirm that preproduction and production use the correct accounts, subscriptions, projects, regions, and state backends. Treat a mistaken target as a release-blocking issue.
- Plan visibility: Make the proposed plan available to reviewers and ensure the applied configuration corresponds to the reviewed commit.
- Destructive actions: Look specifically for resource replacement, deletion, changes to network access, database settings, identity permissions, and storage retention.
- Outputs and dependencies: Check that outputs do not expose sensitive values unnecessarily and that dependencies are represented explicitly rather than created through naming assumptions.
Infrastructure review should also account for what happens after apply. If a change affects deployment capacity, application behavior, or operational signals, pair it with an appropriate validation plan. The preproduction observability guide can help connect infrastructure changes to logs, metrics, traces, and deployment signals.
Common mistakes
Using workspaces as the only environment boundary
Workspaces can be useful when the configuration is genuinely identical and only state is different. They can be less transparent when production and preproduction require different providers, permissions, resources, or approval paths. If a command can target a sensitive environment by changing one easy-to-miss argument, use separate root directories or repositories where that improves safety and clarity.
Copying entire environments without a module boundary
Copy-and-edit layouts are easy to start but tend to drift. If two environments create the same logical components, place the shared composition in a module or shared root module and keep environment-specific values at the edge. Copy configuration only when the systems have intentionally diverged.
Making modules too generic
A module with dozens of switches can become a second programming language that every caller must learn. Prefer a small, opinionated interface and create a new module when the infrastructure pattern is meaningfully different.
Applying from a developer laptop without a clear control model
Local plans are valuable for exploration, but team environments benefit from a consistent CI/CD execution path. Decide who or what may apply, how credentials are provided, how approvals are recorded, and how to recover from a failed change. These rules should be documented alongside the project.
Ignoring operational dependencies
Terraform can create infrastructure without proving that the application, policies, secrets, or monitoring configuration work together. Coordinate infrastructure changes with deployment checks, contract tests, and configuration reviews. For related workflows, see the guides on preproduction secrets and configuration management and API contract testing before production.
When to revisit
Review the project structure before a seasonal planning cycle, before adding a new environment, and whenever ownership or deployment boundaries change. Revisit it after adopting a new backend, changing cloud accounts, introducing a platform team, or expanding the number of services managed by Terraform.
Also schedule a review when Terraform or provider versions change materially, when module interfaces are revised, or when a plan repeatedly surprises reviewers. Check whether state is still divided along useful ownership and lifecycle boundaries, whether CI applies the intended commit, and whether environment differences are documented rather than hidden in conditionals.
Use this short action list before your next infrastructure change:
- Identify the root configuration and state location that the change will affect.
- Confirm the target environment, account, region, and credentials.
- Run formatting, validation, and a plan using the project’s documented versions.
- Review replacements, deletions, access changes, and sensitive outputs first.
- Confirm application, secrets, monitoring, and rollback implications.
- Merge only after the plan has an appropriate review, then apply through the agreed workflow.
- Record any unexpected behavior and use it to improve the module, checklist, or repository structure.
A Terraform project should make the safe path easy to identify. Clear modules, explicit environments, protected remote state, and consistent review workflows provide that foundation while leaving room to adapt as the infrastructure and team evolve.