- Understand what lifecycle meta-arguments are and how they modify Terraform’s default behavior.
- Learn how
create_before_destroychanges the replacement strategy to reduce downtime. - Learn how
prevent_destroyacts as a safety mechanism to block accidental deletions. - Learn how
ignore_changeslets Terraform ignore specified attribute changes during plans and applies.
What are lifecycle meta-arguments?
Lifecycle meta-arguments are specified in alifecycle block inside a resource. They change Terraform’s default replacement and update behavior on a per-resource basis, enabling fine-grained control over create/update/destroy semantics.
Common lifecycle meta-arguments:
Example
lifecycle block structure:
create_before_destroy
Default replacement strategy: when a resource requires replacement, Terraform typically destroys the existing resource first and then creates the new one. For resources that cannot be recreated with the same identifiers (name, IP, etc.), this destroy-first behavior can cause downtime. Settingcreate_before_destroy = true flips the replacement order so Terraform attempts to create the new resource before destroying the old one. This reduces downtime for many resources that can coexist temporarily.
Example:
- Provider constraints: Not all resources can live side-by-side. If a provider or resource type requires a unique identifier (for example, a unique DNS name), a create-before-destroy replacement may not be possible and the provider will fall back to destroying first.
- Stateful services:
create_before_destroycan be useful for stateful services when brief overlap is acceptable, but you must evaluate application-level consistency (for example, database replication or cluster reconfiguration). - Plan visibility: Terraform will show a replacement plan that includes a create action followed by a destroy action. Review the plan carefully before applying.
prevent_destroy
prevent_destroy = true makes any planned destroy of the resource fail the plan/apply with an error. This is a defensive setting to protect critical resources from accidental deletion.
Example:
prevent_destroy will block any planned destroy, including terraform destroy for that resource. To remove such a resource you must first remove or change the prevent_destroy setting and then run terraform apply. Do not rely on it as the only form of protection — combine with IAM and organizational safeguards.ignore_changes
ignore_changes tells Terraform to treat specified attributes as if they had not changed when diffing configuration vs. real-world infrastructure. This is helpful when attributes are managed outside Terraform, by provider automation, or by separate tooling.
Example:
- Attribute paths:
ignore_changesaccepts a list of attribute paths. Use dot notation for nested attributes (for example,"metadata.0.name"for certain resources), and quoted strings inside the list for each path. - Granularity: Avoid ignoring entire maps unless necessary. Overusing
ignore_changescan hide drift and lead to unmanaged differences between configuration and actual resources. If you only need to ignore specific map keys (e.g., one tag), prefer provider tag management features or targeted attribute paths if supported. - Workflow: When a resource has
ignore_changes, Terraform will still read the real state but will suppress diffs for the specified attributes, so no update will be planned for those attributes even if they differ.
Best practice: Use lifecycle meta-arguments sparingly and document their intent. They are powerful tools to align Terraform behavior with operational realities but can also mask drift or create surprising behavior if used without care.
Combining lifecycle arguments
Multiple lifecycle settings can be combined in the same resource. Be aware they can interact or conflict. Example:- Conflicts:
create_before_destroyaims to create a replacement before destroying the existing resource. Ifprevent_destroy = trueis also set, the subsequent destroy step will be blocked, preventing the replacement from completing. Test such combinations in non-production environments. - Provider behavior: Always validate provider-specific constraints. Some providers may ignore
create_before_destroyfor certain resource types or require additional configuration to allow parallel resources.
Quick reference
Summary
Lifecycle meta-arguments let you:- Reduce downtime with
create_before_destroy. - Protect critical resources with
prevent_destroy. - Avoid noisy diffs for provider- or externally-managed attributes with
ignore_changes.
Links and References
- Terraform lifecycle meta-arguments: https://www.terraform.io/docs/language/meta-arguments/lifecycle
- Terraform documentation: https://www.terraform.io/docs/
- Best practices and provider docs (example — AWS): https://registry.terraform.io/providers/hashicorp/aws/latest/docs