Interview Prep/Real-World Interview/Write-Terraform tasks

Use `locals`, `terraform.workspace` or tfvars to vary instance size by environment.

IntermediateHands-onWrite-Terraform tasks
Answer
variable "env" { type = string }

locals {
  sizes = {
    dev     = "t3.micro"
    staging = "t3.small"
    prod    = "m6i.large"
  }
  instance_type = lookup(local.sizes, var.env, "t3.micro")
  common_tags = {
    Environment = var.env
    ManagedBy   = "terraform"
  }
}
terraform plan -var-file=envs/prod.tfvars

Key trick → A map plus lookup keeps the environment logic in one place. Many teams prefer separate directories or state per environment over workspaces, so a wrong workspace select can't apply dev changes to prod.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-Terraform tasks