liveBook · Manning

Resource lifecycle block

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

A nested Terraform configuration block that contains rules governing how a resource is created, updated, replaced, and destroyed.

When replacing infrastructure, Terraform normally destroys the existing object before creating its replacement. A resource lifecycle block lets configuration change that behavior and govern other management rules. section 2.7.2

Use the block once inside a resource or data source to configure:

  • create_before_destroy, which creates the replacement first when temporary resource loss is unacceptable, though unique names or shared resources can make this fail. section 2.7.2
  • prevent_destroy, which makes plans that destroy the resource fail; use it rarely because lifecycle values must be hardcoded literals. section 2.7.2

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Explains block scope, replacement ordering, destruction protection, ignored changes, and replacement triggers with configuration examples.
Examples
Uses AWS instances, AMIs, subnets, IAM roles, elastic IP addresses, null resources, and lifecycle argument listings.
What it assumes
Assumes familiarity with Terraform resources and replacement plans while defining lifecycle-block rules directly through examples.
What you can do after
Operate/configure Terraform resources with deliberate replacement, protection, change-ignoring, and replacement-trigger policies.
How it connects
Connects Terraform resources and data sources with replacement behavior, dependencies, resource identity, and infrastructure availability.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.