create_before_destroyprevent_destroyignore_changes
Lifecycle settings change how Terraform performs create/replace/delete operations; use them to control replacement order, protect resources, or ignore drift on attributes you don’t want Terraform to manage.
Quick comparison
How lifecycle affects Terraform behavior
Lifecycle rules change how Terraform plans and applies operations, but they do not change the underlying provider resource behavior (for example, how an AWS or GCP service itself behaves at runtime). Some providers or resource types may not support creating new and keeping old resources simultaneously; Terraform will only honor the lifecycle semantics when feasible.create_before_destroy
By default, Terraform may destroy the old resource before creating the replacement. Settingcreate_before_destroy = true tells Terraform to attempt creating the new resource first, then destroy the old resource only after the new one is successfully created (if the provider supports that workflow).
Example:
- When you need to minimize downtime during replacement.
- When your provider and resource support multiple concurrent resources (e.g., distinct names or IDs).
- Some resources cannot be created in parallel due to naming constraints or provider limitations; Terraform will only apply
create_before_destroywhen feasible. - For resources that must be unique (by name or IP),
create_before_destroymay not be possible.
prevent_destroy
prevent_destroy = true protects a resource from being removed. If a plan would delete a resource with prevent_destroy enabled, Terraform will error instead of performing the deletion.
Example:
prevent_destroy = true is a strong safeguard. To remove a protected resource you must explicitly change the configuration (for example, remove the lifecycle block or set prevent_destroy = false) and apply the change, or carefully manipulate the state. Avoid manipulating state unless you understand the risks.- Protect production resources such as databases, primary storage buckets, or critical networking resources.
- Prevent accidental deletion during refactoring or automation mistakes.
prevent_destroyaffects Terraform’s plan/apply flow, not the provider’s inherent delete safeguards.
ignore_changes
ignore_changes tells Terraform to ignore changes to one or more resource attributes when planning. This is useful when those attributes are managed outside Terraform (for example, by external controllers, autoscalers, or manual edits) and you do not want Terraform to continually try to revert them.
Example — ignore simple attributes:
- Use attribute names or attribute-path strings in the list. For nested/list attributes, specify the path as a string (for example,
"metadata.0.annotations"). - Ignored attributes are skipped during diff calculations and Terraform will not plan changes to them; other non-ignored attributes continue to be reconciled normally.
- Do not use
ignore_changesas a substitute for proper resource ownership. If Terraform should be the authoritative manager of a field, do not ignore it. - Overusing
ignore_changescan hide configuration drift and complicate troubleshooting.
Examples and patterns
- Minimal downtime replacement:
Use
create_before_destroy = trueon resources that can exist concurrently (e.g., some compute instances, DNS records if using a separate name). - Protecting stateful services:
Add
prevent_destroy = trueto databases, storage buckets, or identity resources where accidental deletion would be catastrophic. - Handling external controllers:
Use
ignore_changesfor fields managed by external systems (service mesh injections, autoscaling group sizes managed by an autoscaler, etc.).
Summary
- Lifecycle meta-arguments change how Terraform manages resources during create/replace/delete operations.
- They affect Terraform’s management and planning behavior, not the provider’s runtime behavior.
- Use
create_before_destroyto prefer new-before-old replacement,prevent_destroyto block accidental deletions, andignore_changesto avoid reconciling attributes managed externally. - Apply lifecycle rules thoughtfully — they can prevent outages, but misusing them can hide drift or block legitimate changes.