liveBook · Manning

Prevent_destroy

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

prevent_destroy is a Terraform lifecycle setting that causes plans destroying the associated resource to fail when enabled.

Terraform’s prevent_destroy lifecycle setting makes any plan that would destroy its associated resource fail. Enable it with the literal value true inside a resource’s lifecycle block. section 2.7.2

Protecting an instance

A resource can reject destructive plans by declaring lifecycle { prevent_destroy = true }. This may suit logs that must remain for compliance, but it should be used rarely because it obstructs planned destruction. section 2.7.2

The setting accepts only hardcoded values, so it cannot vary between production and development. Removing the resource block also removes the setting, allowing Terraform to destroy the resource. section 2.7.2

For accidental-update protection, consider ignore_changes instead; prevent_destroy is primarily a hard stop against destruction and can hinder temporary environment workflows. section 2.7.2

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

How deep it goes
Explains literal-only configuration, block-removal behavior, and operational drawbacks, but does not detail Terraform’s plan internals.
Examples
Uses an AWS instance configuration and compliance-related logs as practical examples.
What it assumes
Assumes familiarity with Terraform resources and lifecycle blocks while defining prevent_destroy directly.
What you can do after
Operate/configure Terraform resources with a hardcoded safeguard against planned destruction.
How it connects
Connects lifecycle rules with ignore_changes and resource destruction.

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.