Interview Prep/Real-World Interview/Multi-env and multi-account

Someone ran `terraform apply` with dev variables against the prod state. How do you make that impossible?

BeginnerScenarioMulti-env and multi-account
Answer

Situation read → Humans running Terraform locally with broad credentials will eventually hit the wrong environment. Remove the possibility, don't rely on care.

What I'd do →

  1. Separate state and backend per environment (separate directories or accounts), not a variable switch.
  2. Separate AWS accounts with separate roles; a dev role simply has no rights in prod.
  3. Assert the account inside Terraform so a mismatch fails at plan:
provider "aws" {
  region              = "ap-south-1"
  allowed_account_ids = ["444455556666"] # prod
}
  1. Prod applies only from CI after a reviewed plan; humans get read-only in prod.
  2. Recover from this one: check the plan or apply output, reconcile with a fresh plan, and restore the previous state version from S3 versioning if state was damaged.

Why → allowed_account_ids turns "wrong credentials" into an immediate error instead of a change.

Interview tip → Mention allowed_account_ids; few candidates know it exists.

All Real-World Interview questions Roadmap: Interview readiness

More on Multi-env and multi-account