Repository layouts for Terraform

Beginner · 9 min

Where Terraform code lives in a repo, how modules and live configuration split, and what to commit, with a script that catches a shared state key.

Before thisWhat modules are and the root moduleRemote backends: S3, GCS and azurerm

Josh Geissler · 9 min

Repository layouts for Terraform

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Josh GeisslerReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • One directory is one root module and one state, so the layout decides how large a plan is and what one mistake can reach.
  • Keep reusable modules apart from thin live configuration: one root module per environment and component, each with its own backend key.
  • Commit .terraform.lock.hcl and environment sizing tfvars; ignore .terraform/, state files and tfvars that hold secrets.
  • A copied environment directory that keeps the old backend key shares state with the original, and each directory is valid on its own, so nothing in Terraform flags it. Check keys in CI.

The 1,400-line main.tf

Every Terraform repo starts as one directory with one main.tf. It works for a week. Then someone adds a database, someone else adds a queue, a second environment arrives, and the file reaches 1,400 lines. Two people edit it in the same pull request. A plan takes four minutes because it refreshes every resource you own. Nobody can say which part of the file production depends on.

A repository layout is the answer to a question you will be asked sooner than you expect: where does this change go, and what does it touch? The layout decides the size of your plans, who can review what, and how much damage one typo can do. This lesson is the map. The next four lessons dig into the parts: workspaces, directories and tfvars, Terragrunt and splitting state.

One directory is one state

Terraform works on one directory at a time, the root module, and each root module has its own state. That fact drives every layout decision. If two things must be planned and applied together, they live in the same root module. If they should be able to change independently, they probably should not.

The files inside a directory are for humans only. Terraform reads every *.tf file in the directory as one configuration, so main.tf, variables.tf and outputs.tf are convention, not mechanism. The convention is worth following because it is what everyone expects, and it is the shape a module has too:

network/
  main.tf          # resources
  variables.tf     # inputs
  outputs.tf       # what other code may read
  versions.tf      # required_version, required_providers
  README.md

Split main.tf by topic (dns.tf, iam.tf) once it passes a few hundred lines. Do not split it into directories until the pieces need separate state.

The layout most teams converge on

Two kinds of code, kept apart:

  • Modules: reusable building blocks with inputs and outputs. They describe a shape (“a web service”) and know nothing about environments.
  • Live configuration: the root modules you actually run, one per environment and component. They are thin: a backend, a provider, some module calls and the values that make this copy different from the others.
infra/
  modules/
    network/
    web-service/
  envs/
    dev/
      network/        # state key: dev/network/terraform.tfstate
      app/
    prod/
      network/
      app/

That is the same tree the devops lesson on modules, environments and remote state ended with. Here it is as one decision table.

Choice Fits Costs
One root module, one state A small project, one environment Plans slow down, blast radius is everything
modules/ plus envs/<env>/<component> in one repo Most teams up to a few dozen engineers Some repetition in the thin root modules
Modules in their own repos, tagged, live config in another Several teams sharing modules Changing a module takes a tag, a PR and a bump
A repo per environment Strict access separation Drift between repos, many PRs per change
A branch per environment Almost nothing Branches diverge and merges stop being promotions

Pin module versions when modules live in another repo, and use relative paths (source = "../../modules/network") when they live in the same one. A relative path means a change to the module is a change to every environment that calls it in the same pull request, which is either what you want or the next incident. Module sources and versioning covers the pinning syntax.

What to commit and what to ignore

Commit .terraform.lock.hcl, because it pins provider versions and checksums for everyone and for CI, as the lock-file lesson explains. Ignore .terraform/ (downloaded providers and modules), *.tfstate and *.tfstate.backup (state, which may hold secrets), and any *.tfvars that carries secrets or crash.log. Commit .tfvars files that only hold environment sizing; they are how an environment differs from its siblings.

A layout bug a script can catch

The most common mistake in the directory-per-environment layout is a copy-paste one: you copy envs/dev/app to envs/prod/app and forget to change the backend key. Both directories now read and write the same state. The first apply in prod tries to make dev’s resources match prod’s config.

Each directory is valid on its own, and I know of no check in Terraform that compares them. I wrote a short Python script that walks a tree, pulls the S3 backend bucket and key out of each backend.tf with a regex, and reports duplicates. It is a stand-in for a check you would put in CI. I ran it on a four-directory tree I built by hand, after changing prod/app’s key back to dev’s:

$ python3 -I lintkeys.py infra
s3://acme-tf-state/dev/app/terraform.tfstate  <- ['envs/dev/app', 'envs/prod/app']  resources=4  <-- COLLISION
s3://acme-tf-state/dev/network/terraform.tfstate  <- ['envs/dev/network']  resources=2
s3://acme-tf-state/prod/network/terraform.tfstate  <- ['envs/prod/network']  resources=2

It exits non-zero on a collision, so a CI step can fail the pull request. The same idea is why some teams generate the backend block instead of writing it, which is one of the things Terragrunt does.

About the video

The embed is Josh Geissler’s “How To Structure Your Terraform Projects (Don’t Make This Mistake)”, 8 minutes 49 seconds, published March 2025. Its chapters run from a deliberately bad structure through a small project and Terraform git control to a multi-environment layout. I judged it English from the title, description and the channel’s English auto-caption track, and I took what it covers from the title, description and chapter list, not from watching all of it. Anton Putra’s longer video in deeper walks through three levels of project structure in detail.

The HCL and trees in this lesson are conventions written from the module documentation. I did not run Terraform here, only the key-collision script above.

Where this fits

A layout is only as good as its seams. The next lesson takes the shortcut people reach for first, workspaces, and shows where it stops being a layout at all.

Check yourself

  1. 01Why does a directory per environment and component shrink the blast radius?

    Each directory is its own root module with its own state, so a plan reads only that component, and CI can give each one its own credentials.

  2. 02You copy envs/dev/app to envs/prod/app and run apply in prod. What is the most likely hidden mistake?

    The backend key was not changed, so prod reads and writes dev's state. A CI check that finds duplicate bucket and key pairs catches it.

  3. 03Which files do you commit and which do you ignore?

    Commit .terraform.lock.hcl and tfvars that only hold sizing. Ignore .terraform/, *.tfstate files and tfvars with secrets.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark