๐Ÿ”— Repo: github.com/Push/terraform-aws-labs/day06

๐ŸŽฏ Goal: Move from monolithic main.tf โ†’ organized, maintainable, team-ready project layout.


๐Ÿง  Why Structure Matters

๐Ÿ’ก Terraform merges all .tf files into one config โ€” but how you organize them determines:

โŒ Bad: One main.tf with 2,000 lines

โœ… Good: Logical, consistent, documented structure


๐Ÿ“ Production-Grade File Structure (Recommended)

๐Ÿ”น Core Layout (/day06/)

day06/
โ”œโ”€โ”€ backend.tf           # ๐Ÿ” Remote state config (S3 + locking)
โ”œโ”€โ”€ provider.tf          # ๐ŸŒ AWS provider + default tags
โ”œโ”€โ”€ variables.tf         # ๐Ÿ“ฅ Input variables (with validation!)
โ”œโ”€โ”€ locals.tf            # ๐Ÿงฎ Computed values (DRY logic)
โ”œโ”€โ”€ main.tf              # ๐Ÿ—๏ธ *Only* top-level resources (or empty!)
โ”œโ”€โ”€ vpc.tf               # ๐ŸŒ Networking (VPC, subnets, IGW)
โ”œโ”€โ”€ storage.tf           # ๐Ÿชฃ S3, EBS, EFS
โ”œโ”€โ”€ outputs.tf           # ๐Ÿ“ค Exposed values (VPC ID, bucket ARN)
โ”œโ”€โ”€ terraform.tfvars     # ๐ŸŽ›๏ธ Default values (dev)
โ”œโ”€โ”€ .gitignore           # ๐Ÿšซ Block sensitive files
โ””โ”€โ”€ README.md            # ๐Ÿ“ Project docs

โœ… Key Principle:

โ€œSeparation of Concernsโ€ โ€” group by function, not just resource type.


โœ๏ธ Step-by-Step: Refactor Day 5 โ†’ Day 6

1๏ธโƒฃ backend.tf โ€” Remote State (Isolated & Secure)

# ๐Ÿ” backend.tf
terraform {
  required_version = ">= 1.5.0"
  required_providers {
    aws = { source = "hashicorp/aws", version = "~> 6.7.0" }
  }

  # โœ… S3 backend (encrypted + locked)
  backend "s3" {
    bucket         = "tech-tutorials-push-terraform-state-2025"
    key            = "dev/terraform.tfstate"
    region         = "us-east-1"
    encrypt        = true
    use_lock_file  = true  # ๐Ÿ”’ Native S3 locking (no DynamoDB!)
  }
}

โœ… Why separate?