Terraform Associate 004 ยท Dependencies, validation and sensitive data
The lifecycle meta-argument
create_before_destroy, prevent_destroy, ignore_changes, replace_triggered_by (4f).
Key points
- lifecycle tunes how Terraform plans a resource. create_before_destroy builds the replacement first. prevent_destroy errors on any plan that would destroy the resource (deleting the block itself still removes it). ignore_changes lists attributes Terraform should not try to revert. replace_triggered_by forces replacement when a referenced resource or attribute changes.
- Values must be literals.
Source: https://developer.hashicorp.com/terraform/language/block/resource
Practice questions
Which lifecycle argument makes Terraform build a replacement object before removing the old one?
- create_before_destroy
- prevent_destroy
- ignore_changes
- replace_triggered_by
Show answer
Answer: A. create_before_destroy
create_before_destroy reverses the default destroy-then-create order. prevent_destroy blocks destruction, ignore_changes skips attributes, replace_triggered_by forces replacement.
Which statements about prevent_destroy are true?
- A plan that would destroy the resource is rejected
- Removing the resource block from the configuration is not stopped by it
- It prevents other tools from deleting the real object
- It can be set from an input variable value
- It also stops in-place updates
Show answer
Answer: A. A plan that would destroy the resource is rejected, B. Removing the resource block from the configuration is not stopped by it
A plan that would destroy the resource is rejected with an error. Deleting the resource block from the configuration is not blocked. It is a Terraform-level guard and cannot stop changes made outside Terraform.
Setting prevent_destroy = true keeps a resource safe even if you delete its block from the configuration.
Show answer
Answer: False
False. The guard only rejects plans that destroy a resource still in the configuration; removing the block removes the protection too.
Practice 8 questions on this topic in Bitzu
Short lessons, instant explanations and spaced review of what you get wrong. Free on iOS and Android.
