Skip to main content
In this lesson, we’ll dive into OpenTofu’s lifecycle rules to control how resources are created, updated, or destroyed. Proper use of these rules helps you avoid downtime, accidental deletions, and unnecessary modifications.

Table of Contents


Default Behavior

By default, OpenTofu will destroy and then recreate any resource if a change requires replacement (for example, updating an AMI in an aws_instance).
After changing the ami value and running tofu apply, you’ll see:

1. create_before_destroy

Use create_before_destroy = true to provision the replacement resource before tearing down the old one. This helps minimize downtime.
Running tofu apply now provisions the new instance first:

2. prevent_destroy

Set prevent_destroy = true to block any accidental deletion during an apply. OpenTofu will throw an error if a change requires replacement.
If you change the AMI and run tofu apply, you’ll get:
prevent_destroy does not block a direct tofu destroy. It only prevents destruction during apply operations triggered by configuration changes.

3. ignore_changes

The ignore_changes meta-argument instructs OpenTofu to skip tracking specified attributes, even if they drift from your configuration.
If someone updates the Name tag manually in the AWS console, tofu apply reports no changes:
You can ignore multiple attributes, or even all changes:
Use ignore_changes = all cautiously. Ignoring all drift may mask unintended configuration drift over time.

Summary of Lifecycle Rules


Further Reading

Watch Video