DEV Community

Cover image for πŸš€ Terraform Directory Structure – The Right Way!
Surender Gupta
Surender Gupta

Posted on

πŸš€ Terraform Directory Structure – The Right Way!

When building cloud infrastructure using Terraform, your directory structure matters more than you think.

A well-structured Terraform layout helps you:

  • Scale easily across environments
  • Reuse code with modules
  • Avoid production mistakes
  • Automate safely with scripts

In this post, I’ll show you the best practices I follow for organizing Terraform projects used in Dev, Stage, and Prod.


πŸ“ 1. Environment Separation: Dev, Stage, Prod

Organize each environment in its own folder:

terraform/
β”œβ”€β”€ dev/
β”‚   β”œβ”€β”€ main.tf
β”‚   β”œβ”€β”€ variables.tf
β”‚   β”œβ”€β”€ outputs.tf
β”œβ”€β”€ stage/
β”‚   └── ...
β”œβ”€β”€ prod/
β”‚   └── ...
└── env/
    β”œβ”€β”€ dev.tfvars
    β”œβ”€β”€ stage.tfvars
    └── prod.tfvars
Enter fullscreen mode Exit fullscreen mode

βœ… Pro tip: Keep .tfvars values in a separate env/ directory for cleaner management.

Why it matters:

  • Prevents accidental changes to production
  • Clear separation of responsibilities
  • Easier CI/CD integration

πŸ“¦ 2. Reuse Code with Modules

Instead of copying and pasting the same code, create reusable modules:

modules/
β”œβ”€β”€ vpc/
β”‚   β”œβ”€β”€ main.tf
β”‚   β”œβ”€β”€ variables.tf
β”‚   └── outputs.tf
β”œβ”€β”€ ec2/
β”‚   └── ...
Enter fullscreen mode Exit fullscreen mode

Usage example:

module "vpc" {
  source     = "../../modules/vpc"
  cidr_block = var.vpc_cidr
}
Enter fullscreen mode Exit fullscreen mode

Benefits:
πŸ’‘ Define once, use everywhere
πŸ”„ Ensures consistency across environments
🧩 Simplifies testing and debugging


πŸ€– 3. Automate with Scripts

Use shell scripts to handle Terraform operations like init, apply, and destroy.

scripts/
β”œβ”€β”€ init.sh        # terraform init
β”œβ”€β”€ plan.sh        # terraform plan
β”œβ”€β”€ apply.sh       # terraform apply
└── teardown.sh    # terraform destroy
Enter fullscreen mode Exit fullscreen mode

Why script it?
⏱️ Saves time
πŸ” Reduces manual error
βš™οΈ Fits into CI/CD pipelines seamlessly


🧠 4. Core Terraform Files

Each environment/module folder typically contains:

  • main.tf: Infrastructure definitions
  • variables.tf: Variable declarations
  • outputs.tf: Exposed outputs
  • terraform.tfvars: Variable values (optional if using env/*.tfvars)
  • provider.tf: Cloud provider setup
  • backend.tf: Remote state configuration

πŸ›‘οΈ Store state remotely (e.g., S3 + DynamoDB) for security and team collaboration.


πŸ–ΌοΈ Visual: Terraform Directory Structure

Terraform Directory Structure

Infographic: High-level layout of a scalable Terraform project


βœ… Summary: Why This Structure Works

βœ… Modular and reusable
βœ… Safer deployments
βœ… DevOps- and CI/CD-friendly
βœ… Environment-ready (Dev, Stage, Prod)


πŸ’¬ What’s your Terraform setup look like?

Do you structure things differently?

Have tips for managing multiple environments or state files?

Let’s share ideas in the comments πŸ’¬πŸ‘‡


#Terraform #DevOps #InfrastructureAsCode #AWS #GCP #Azure #CloudEngineering #SRE #CI/CD #IaC

Top comments (0)