002 · Domain 5: Terraform Security and Governance · 13% of the exam
Task 5.5: Identify and mitigate risky infrastructure changes
Spotting a dangerous plan and stopping it: replace and destroy markers, prevent_destroy, create_before_destroy, ignore_changes, sensitive values in plans, and preconditions that block bad inputs.
Study it
Risky plans: replacements, destroys and the lifecycle rules that stop them
Lesson coming
Sample questions
Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.
Question 1 · choose 1
To avoid an outage when a load balancer target group is replaced, the team adds lifecycle { create_before_destroy = true }. The next change that forces replacement fails because a target group with that name already exists. The name is set with name = "app-tg". What should the team do?
AAdd create_before_destroy to every resource the target group depends on as well
BUse the resource's name-prefix option instead of a fixed name to let old and new coexist
CSet prevent_destroy on the target group to keep the old object until the new one works
DRun terraform state rm on the old target group first to let a replacement with the same name be created
Show the answer and why
AAdd create_before_destroy to every resource the target group depends on as well
Incorrect
The failure is the name that both target groups need at the same time; changing other resources' lifecycle settings does not free it.
BUse the resource's name-prefix option instead of a fixed name to let old and new coexist
Correct
With create_before_destroy both objects exist at once, so names that must be unique need an option that adds a unique suffix.
CSet prevent_destroy on the target group to keep the old object until the new one works
Incorrect
prevent_destroy makes Terraform reject any plan that destroys the object, so the replacement could never complete.
DRun terraform state rm on the old target group first to let a replacement with the same name be created
Incorrect
state rm only forgets the old object; it still exists with the same name, so creating the new one still conflicts and the old one is left unmanaged.
create_before_destroy is opt-in because many objects have unique-name constraints. Before enabling it, make sure two copies can exist at once, for example with a generated suffix.
A refactor changes aws_instance.app from count = 3 to for_each over the keys "blue", "green" and "canary". The plan shows 3 to add and 3 to destroy. Which change lets the existing servers keep running under their new keys?
ARun terraform apply -refresh-only to record the new keys in state before the normal apply
BAdd lifecycle { ignore_changes = all } to the resource during the switch
CAdd lifecycle { prevent_destroy = true } to make Terraform keep the old instances
DAdd moved blocks from aws_instance.app[0], [1] and [2] to the matching keys
Show the answer and why
ARun terraform apply -refresh-only to record the new keys in state before the normal apply
Incorrect
Refresh-only mode updates attribute values from real objects; it does not move instances to new addresses.
BAdd lifecycle { ignore_changes = all } to the resource during the switch
Incorrect
ignore_changes only suppresses updates to arguments; it has no effect on instances whose address changes.
CAdd lifecycle { prevent_destroy = true } to make Terraform keep the old instances
Incorrect
prevent_destroy makes the plan fail; it does not map old instances to new keys.
DAdd moved blocks from aws_instance.app[0], [1] and [2] to the matching keys
Correct
moved blocks with instance keys tell Terraform which old index becomes which new key, so it renames instead of replacing.
Read the plan's add and destroy counts as a warning sign after any refactor. moved blocks record address changes, including index-to-key changes between count and for_each.