Interview Prep/Terraform/Import Refactoring Drift and Lifecycle

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 →

  1. Use name_prefix so Terraform generates a unique suffix.
  2. 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.

All Terraform questions Roadmap: Terraform in production

More on Import Refactoring Drift and Lifecycle