DEV Community

Aisalkyn Aidarova
Aisalkyn Aidarova

Posted on

PROJECT: Terraform Workspaces – Dev / Stage / Prod (Production Style)

Project Goal

  • Use ONE Terraform codebase
  • Create multiple environments using workspaces
  • Keep separate state files
  • Avoid code duplication
  • Follow Terraform Associate exam expectations

What this project simulates (real world)

A company wants:

  • dev, stage, prod
  • Same EC2 setup
  • Same AWS account
  • Same region
  • Different naming, tags, size

✔ This is exactly what workspaces are designed for.


1️⃣ Project Structure

terraform-workspaces-project/
│
├── main.tf
├── variables.tf
├── locals.tf
├── outputs.tf
├── providers.tf
Enter fullscreen mode Exit fullscreen mode

👉 One folder
👉 One codebase
👉 No duplication


2️⃣ providers.tf

provider "aws" {
  region = "us-east-1"
}
Enter fullscreen mode Exit fullscreen mode

📌 Exam note:

  • Workspaces do NOT change providers
  • Same account, same region

3️⃣ variables.tf

variable "ami_id" {
  description = "AMI ID for EC2"
  type        = string
}

variable "instance_type_map" {
  description = "Instance type per environment"
  type        = map(string)
  default = {
    dev   = "t2.micro"
    stage = "t2.small"
    prod  = "t3.medium"
  }
}
Enter fullscreen mode Exit fullscreen mode

📌 Production concept:

  • Env-based configuration
  • No hardcoding

4️⃣ locals.tf (KEY FILE – EXAM FAVORITE)

locals {
  env = terraform.workspace

  instance_type = lookup(
    var.instance_type_map,
    local.env,
    "t2.micro"
  )
}
Enter fullscreen mode Exit fullscreen mode

📌 What this teaches:

  • terraform.workspace
  • lookup() function
  • Safe defaults
  • Dynamic behavior per env

5️⃣ main.tf

resource "aws_instance" "app" {
  ami           = var.ami_id
  instance_type = local.instance_type

  tags = {
    Name        = "app-${terraform.workspace}"
    Environment = terraform.workspace
  }
}
Enter fullscreen mode Exit fullscreen mode

📌 Result:

  • Same code
  • Different EC2s
  • Different names
  • Different sizes

6️⃣ outputs.tf

output "instance_id" {
  value = aws_instance.app.id
}

output "environment" {
  value = terraform.workspace
}
Enter fullscreen mode Exit fullscreen mode

7️⃣ How RUN the Project (STEP BY STEP)

Step 1: Init

terraform init
Enter fullscreen mode Exit fullscreen mode

Step 2: Create workspaces

terraform workspace create dev
terraform workspace create stage
terraform workspace create prod
Enter fullscreen mode Exit fullscreen mode

Step 3: Deploy DEV

terraform workspace select dev
terraform apply
Enter fullscreen mode Exit fullscreen mode

✔ Creates:

  • EC2 name: app-dev
  • Type: t2.micro

Step 4: Deploy STAGE

terraform workspace select stage
terraform apply
Enter fullscreen mode Exit fullscreen mode

✔ Creates:

  • EC2 name: app-stage
  • Type: t2.small

Step 5: Deploy PROD

terraform workspace select prod
terraform apply
Enter fullscreen mode Exit fullscreen mode

✔ Creates:

  • EC2 name: app-prod
  • Type: t3.medium

8️⃣ What Terraform Does Internally (IMPORTANT)

Image

Image

Image

terraform.tfstate.d/
├── dev/
│   └── terraform.tfstate
├── stage/
│   └── terraform.tfstate
├── prod/
│   └── terraform.tfstate
Enter fullscreen mode Exit fullscreen mode

✔ Same code
✔ Separate states
✔ Safe isolation


9️⃣ Why this is PRODUCTION-VALID

Used when:

  • Same AWS account
  • Same region
  • Same infra shape
  • Different env behavior

NOT used when:

  • Different accounts
  • Different providers
  • Strong isolation required

🔟 Terraform Certificate Mapping (EXPLICIT)

This project covers:

✔ Workspaces
✔ State isolation
terraform.workspace
locals
lookup()
✔ Variable maps
✔ Safe defaults

Exactly what appears in Terraform Associate exam.


1️⃣1️⃣ Interview Answer (MEMORIZE)

Question:
How do you manage dev, stage, and prod using Terraform?

Answer:

I use Terraform workspaces when the infrastructure is the same but environments differ. Each workspace maintains its own state file while sharing the same code. I use terraform.workspace with locals and maps to adjust behavior like instance size and naming per environment.


1️⃣2️⃣ Common Interview Trap (IMPORTANT)

❌ “I use workspaces for different AWS accounts”
✅ Correct answer:

For different accounts or regions, I prefer separate directories and remote state backends.

Why THIS project is strong

  • Real production logic
  • Simple enough for students
  • Direct exam relevance
  • Interview-ready explanation
  • No bad practices

Top comments (0)