Using `create_before_destroy` fails because the new resource's name conflicts with the old one.
IntermediateScenarioImport Refactoring Drift and Lifecycle
Answer
Situation read → Both exist briefly and share a unique name.
What I'd do →
- Use
name_prefixso Terraform generates a unique suffix. - Where the resource has no prefix option, include a changing component such as a random or version suffix.
resource "aws_security_group" "app" {
name_prefix = "app-"
vpc_id = var.vpc_id
lifecycle { create_before_destroy = true }
}
Why → Names must be unique while both versions coexist; a prefix makes that automatic.
Interview tip → Applies to launch templates, security groups and IAM roles.
More on Import Refactoring Drift and Lifecycle
An S3 bucket was created by hand in the console and now must be managed by Terraform. How do you bring it in?BeginnerYou 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`?Beginner