Terraform · Infrastructure as code

How do lifecycle rules change create, update and destroy behaviour?

HardlifecyclereplacementssafetyUpdated Oct 2026
Quiz this track

Short answer

Lifecycle meta-arguments control how Terraform creates, replaces and destroys resources. Use create_before_destroy for zero-downtime replacements, prevent_destroy as a guardrail, and ignore_changes narrowly.

Lifecycle settings apply to one resource block. They do not change the provider API, only Terraform's ordering and safety checks. Used well, they prevent outages and accidental deletion. Used broadly, they hide real drift and create replacement conflicts.

hcl
resource "aws_instance" "app" {
  ami           = data.aws_ami.base.id
  instance_type = "t3.micro"

  lifecycle {
    create_before_destroy = true
    prevent_destroy       = true
    ignore_changes        = [user_data]
  }
}
create_before_destroy keeps capacity during replacement, while prevent_destroy blocks accidental deletion.

The main rules

  • create_before_destroy reverses the default destroy-then-create order
  • prevent_destroy fails the plan when a destroy would occur
  • ignore_changes keeps specified attributes from causing updates
  • replace_triggered_by forces replacement when another resource changes
  • precondition and postcondition validate assumptions during apply

create_before_destroy can fail when names must be unique, because the old and new objects coexist briefly. It also interacts with dependencies that still point at the old object. ignore_changes can mask drift permanently, so scope it to computed or externally managed attributes and revisit it.

prevent_destroy is a guardrail against configuration-driven deletion, not a security control. Someone with state access can still remove the resource from state or edit the block.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1What problem does create_before_destroy solve?
  2. 2Why can create_before_destroy fail with unique names?
  3. 3When is ignore_changes dangerous?
  4. 4What is the difference between precondition and postcondition?

Say it in the interview

Lifecycle rules change Terraform's ordering and safety checks for one resource. I use create_before_destroy when a replacement must keep capacity, prevent_destroy on stateful resources, and ignore_changes only for attributes another system owns. The trade-offs are real: create_before_destroy can collide on unique names, and broad ignore_changes hides drift. Prevent_destroy is a guardrail, not a security boundary.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Terraform mock interview and get scored feedback on depth, clarity and confidence.