You need to move Terraform state from local files to an S3 backend. How?

BeginnerScenarioMigrations
Answer

Situation read → Simple but risky: get it wrong and Terraform thinks nothing exists and tries to recreate everything.

What I'd do →

  1. Create the S3 bucket with versioning and encryption, ideally from a separate bootstrap config.
  2. Add the backend block:
terraform {
  backend "s3" {
    bucket       = "acme-tf-state"
    key          = "prod/network/terraform.tfstate"
    region       = "ap-south-1"
    encrypt      = true
    use_lockfile = true
  }
}
  1. Run terraform init -migrate-state and answer yes to copy the state.
  2. Verify with terraform plan: it should report no changes.
  3. Delete the local state file (keep a backup copy) and make sure *.tfstate is in .gitignore.

Why → use_lockfile uses S3-native locking (Terraform 1.10+), so a DynamoDB lock table is no longer required.

Interview tip → "Plan shows no changes" is the verification step; say it.

All Real-World Interview questions Roadmap: Interview readiness

More on Migrations