linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
Intro
Lessons
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Lessons
  • How it works
  • Knowledge base
  • Cheat sheet
  • Capstone
  • Interview prep
home/terraform/how/tf-resource-lifecycle

how/resources

lifecycle: four levers for managing a resource

create_before_destroy for zero-downtime, prevent_destroy for the critical stuff, ignore_changes for drift, replace_triggered_by for cascading rebuilds. What each one does in practice.

The lifecycle block inside resource is a meta-block. It does not describe what the resource does in the cloud. It describes how Terraform should treat that resource: in what order to create and destroy it, whether to allow changes, what to recreate when dependencies change.

Four arguments, each for its own scenario. Press ▶ and we will walk through them and see what changes in the behavior of apply and destroy.

step 1/6·00 · default: destroy → create
обычный resource - без lifecycle-блокаaws_instance.webami-oldDESTROYaws_instance.webami-new (downtime!)NEWdefault: destroy old → create new. Между ними сервиса нет.дефолт: destroy → create. Простейший случай - без lifecycle

§ steps

  1. Without a lifecycle block Terraform behaves predictably: if an attribute that requires replace (force-new) changes, it first destroys the old instance, then creates the new one.

    Between those two moments the resource does not exist. For an EC2 instance behind a load balancer that is a one-second hiccup. For a database it is a loss of connections and cache. For a unique IP it is a window with no service.

    Next: four ways to influence this behavior.

recap

Quick "when to use what" guide:

  • create_before_destroy = true is for when you need zero-downtime. It fits replacing EC2 instances, AMIs, container tasks. It does not fit when a resource has a unique name or a unique IP/port that cannot exist in two copies at the same time (the classic bottleneck: aws_security_group with the same name).
  • prevent_destroy = true is for production databases, S3 with user data, IAM roles with history. It is a "no" to apply if it tries to delete the resource. To actually tear it down, you first remove the flag by hand and run a new apply, and only then destroy.
  • ignore_changes = [...] is for when drift is expected. For example, auto-scaling changes desired_capacity, the cloud sets its own timestamp tags, someone bolts on tags with another tool. It is a list of field paths where drift is allowed. Do not use it as a "temporary crutch": you will forget, and the field will quietly fall behind.
  • replace_triggered_by = [...] is for when a change to one resource must force the recreation of another. The typical case: aws_launch_template changes, and you need every aws_instance that uses it to restart too (otherwise they keep running the old version).

There is one pitfall they all share: lifecycle cannot be set through a variable. All four fields take only literals. So create_before_destroy = var.zero_downtime will not work. This is a design decision by HashiCorp, because the dependency graph is built before evaluation.

Next: tf-variable-precedence on where Terraform gets variable values from.

§ dig into the knowledge base

  • tf-resource-lifecyclelifecycle: the full reference
  • tf-depends-ondepends_on vs replace_triggered_by
  • tf-drift-detectionDrift detection: when ignore is a bad idea
  • tf-moved-blockmoved: refactoring without recreation

§ try it hands-on

  • ›tf-beginner-09-lifecycle- Lifecycle in practice
  • ›tf-advanced-05-blue-green-migration- Blue-green migration via CBD
Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies