DEV Community

Cover image for πŸ—οΈ Terraform Production Structure (Dev / Staging / Prod) β€” Part 6
Ahkar Swe
Ahkar Swe

Posted on

πŸ—οΈ Terraform Production Structure (Dev / Staging / Prod) β€” Part 6

Terraform Production Structure (Dev / Staging / Prod) β€” Part 6

So far, you've learned:

  • EC2 deployment
  • Variables and outputs
  • Modules
  • Remote backend (S3 + DynamoDB)

Now it’s time to answer a critical question πŸ‘‡

πŸ‘‰ How do real companies organize Terraform projects?


🎯 What You’ll Learn

  • Multi-environment setup (dev / staging / prod)
  • Professional Terraform folder structure
  • How teams manage infrastructure at scale

❗ The Problem

Your current setup might look like this:

main.tf
variables.tf
outputs.tf
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Works for learning
πŸ‘‰ Breaks in production ❌

Why?

  • No environment separation
  • Hard to manage changes
  • Risk of deploying to wrong environment

πŸ—οΈ Production Structure

Here’s how professionals structure Terraform:

terraform-project/
β”‚
β”œβ”€β”€ modules/
β”‚   └── ec2/
β”‚       β”œβ”€β”€ main.tf
β”‚       β”œβ”€β”€ variables.tf
β”‚       └── outputs.tf
β”‚
β”œβ”€β”€ environments/
β”‚   β”œβ”€β”€ dev/
β”‚   β”‚   β”œβ”€β”€ main.tf
β”‚   β”‚   └── backend.tf
β”‚   β”‚
β”‚   β”œβ”€β”€ staging/
β”‚   β”‚   β”œβ”€β”€ main.tf
β”‚   β”‚   └── backend.tf
β”‚   β”‚
β”‚   └── prod/
β”‚       β”œβ”€β”€ main.tf
β”‚       └── backend.tf
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Why This Structure?

1️⃣ Separation of Environments

Each environment is isolated:

  • dev β†’ testing
  • staging β†’ pre-production
  • prod β†’ live system

πŸ‘‰ Prevents mistakes like deploying test code to production.


2️⃣ Reuse with Modules

All environments use the same module:

module "ec2" {
  source = "../../modules/ec2"

  instance_type = "t2.micro"
}
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Consistent infrastructure across environments.


3️⃣ Independent State Files

Each environment has its own backend:

terraform {
  backend "s3" {
    bucket = "tf-state-bucket"
    key    = "dev/terraform.tfstate"
    region = "ap-southeast-1"
  }
}
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Dev and prod never conflict.


πŸ”„ Workflow Example

Deploy to Dev

cd environments/dev
terraform init
terraform apply
Enter fullscreen mode Exit fullscreen mode

Deploy to Production

cd environments/prod
terraform init
terraform apply
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Same code, different environment.


🧠 DevOps Insight

This structure enables:

  • Safe deployments
  • Team collaboration
  • Scalable infrastructure

πŸ‘‰ This is how companies manage cloud at scale.


⚠️ Common Mistakes

❌ Single folder for all environments
❌ Sharing same state file
❌ Hardcoding values


🎯 What You Just Learned

  • Multi-environment Terraform design
  • Production folder structure
  • Environment isolation best practices

πŸ’‘ Final Thought

Infrastructure is not just code.

πŸ‘‰ It’s a system that must be safe, scalable, and predictable.


πŸš€ What’s Next?

Next, we’ll go deeper:

πŸ‘‰ Terraform workspaces vs environments
πŸ‘‰ When to use each approach


πŸ“š Terraform Learning Series

  • Part 1: Introduction
  • Part 2: Setup
  • Part 3: EC2
  • Part 4: Variables
  • Part 5: Modules + Backend
  • Part 6: Production Structure (this post)
  • Part 7: Workspaces vs Environments

Follow for more DevOps content πŸ”₯

Top comments (0)