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 →
- Create the S3 bucket with versioning and encryption, ideally from a separate bootstrap config.
- 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
}
}
- Run
terraform init -migrate-stateand answer yes to copy the state. - Verify with
terraform plan: it should report no changes. - Delete the local state file (keep a backup copy) and make sure
*.tfstateis 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.
More on Migrations
Migrate an application from EC2 to EKS with zero downtime. Walk me through it.AdvancedMigrate a database from on-prem MySQL to Amazon RDS with minimal downtime. How?AdvancedMigrate CI from Jenkins to GitHub Actions for 80 repositories. How do you plan it?IntermediateUpgrade an EKS cluster from 1.30 to 1.31 in production. What's your process?IntermediateMove from a monolith to microservices. What's the DevOps side of the migration?AdvancedMigrate secrets from environment variables in a Git repo to AWS Secrets Manager. How?Beginner