lifecycle meta-argument.
The lifecycle block lets you control how Terraform creates, updates, and destroys resources. Instead of accepting default behavior, lifecycle gives you fine-grained options to protect critical infrastructure, avoid downtime during replacements, and limit churn caused by external changes.
Place a lifecycle block inside a resource and use options such as prevent_destroy, create_before_destroy, ignore_changes, replace_triggered_by, and validation guards like precondition / postcondition. The examples below show common scenarios and best practices.
Use
lifecycle to make Terraform safer and more predictable for sensitive resources—examples include production databases, stateful servers, and any resource where accidental deletion or downtime would be costly.Example: protect a resource and ignore external changes
This example shows two frequentlifecycle uses:
- Prevent accidental deletion with
prevent_destroy = true. - Ignore external modifications to a particular attribute (
vm_size) usingignore_changes.
prevent_destroy = truecauses Terraform to error if a plan attempts to destroy the resource; you must remove the flag to allow deletion intentionally.ignore_changes = ["vm_size"]tells Terraform to ignore external changes to thevm_sizeattribute; Terraform will not attempt to revert those external modifications.
Use
ignore_changes sparingly. Overuse can hide real configuration drift and make troubleshooting harder. Prefer narrow ignores (specific attributes) instead of ignore_changes = ["*"].Lifecycle options at a glance
Understanding the mainlifecycle options helps you answer common operational and exam-focused questions quickly.
Detailed options and examples
create_before_destroy
By default, Terraform may destroy the old resource before creating the new one during replacement, which can cause downtime.create_before_destroy = true flips the order so Terraform brings up the replacement first and then destroys the previous instance.
prevent_destroy
prevent_destroy = true blocks Terraform from destroying the resource; any plan that includes a destroy will fail until you remove this safeguard.
ignore_changes
ignore_changes tells Terraform to disregard drift for specific attributes so external changes do not trigger an update.
replace_triggered_by
replace_triggered_by forces a resource replacement when a referenced resource (or specific attributes) change, even if Terraform would not automatically detect the need for replacement.
precondition and postcondition
Preconditions and postconditions are plan/apply-time validation guards to ensure operations are safe and results meet expectations.preconditionruns during planning before resource creation/modification. If false, Terraform aborts with the provided message.postconditionruns after creation/modification to validate the final state.
Exam and operational tips
- If a question mentions preventing downtime during replacement,
create_before_destroyis usually the correct choice. - For protecting critical resources from accidental deletion, look for
prevent_destroy. - If external systems modify resources (and you want Terraform to ignore those modifications),
ignore_changesis the relevant option. replace_triggered_byis less common but important when one resource must be recreated due to changes in another.- Be aware that
preconditionandpostconditionexist and are used for validation during plan/apply.
lifecycle meta-argument at a high level. It’s a small set of controls but a powerful tool to make Terraform safer and more predictable for sensitive or production infrastructure.