An S3 bucket was created by hand in the console and now must be managed by Terraform. How do you bring it in?
BeginnerScenarioImport Refactoring Drift and Lifecycle
Answer
Situation read → Existing resource, no code, no state entry.
What I'd do →
- Write the resource block that matches the bucket.
- Add an
importblock (Terraform 1.5+). - Run
terraform plan, adjust the code until the diff is empty, then apply.
import {
to = aws_s3_bucket.assets
id = "acme-assets-prod"
}
resource "aws_s3_bucket" "assets" {
bucket = "acme-assets-prod"
}
Why → Import blocks are reviewable and plannable, unlike the one-shot terraform import command.
Interview tip → Import only writes state; you must write the code that matches.
More on Import Refactoring Drift and Lifecycle
You must adopt 200 existing resources and don't want to write all the HCL by hand.IntermediateHow do you find out that someone changed infrastructure outside Terraform?BeginnerSomeone opened port 22 on a security group in the console. Terraform wants to remove it. What's your process?IntermediateAn Auto Scaling group's desired capacity is changed by autoscaling, and Terraform keeps resetting it.IntermediateHow do you protect a production database from an accidental `terraform destroy`?BeginnerA load balancer's TLS certificate must be rotated with no outage.Intermediate