002 · Domain 3: Infrastructure Lifecycle and Operations · 22% of the exam
Task 3.3: Apply changes to infrastructure
Applying safely: saved plans, state locking and force-unlock, what state holds after a partial failure, provisioners as a last resort, and parallelism.
Study it
Applying safely: locks, partial failures and provisioners
Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.
Question 1 · choose 1
A pipeline wants machine-readable apply output and runs terraform apply -json in the apply job without a plan file. The job fails before making any change. What is the fix?
AAdd -input=true to keep the approval prompt available while printing JSON
BAdd -auto-approve, or apply a saved plan file that a reviewer approved earlier
CAdd -compact-warnings; JSON output fails whenever the plan contains warnings of any kind
DReplace -json with -no-color, the machine-readable mode for plan and apply
Show the answer and why
AAdd -input=true to keep the approval prompt available while printing JSON
Incorrect
-json implies -input=false, so no prompt can be shown; the run needs approval some other way.
BAdd -auto-approve, or apply a saved plan file that a reviewer approved earlier
Correct
To use -json with apply, you must also enable -auto-approve or pass a previously saved plan, which counts as approval.
CAdd -compact-warnings; JSON output fails whenever the plan contains warnings of any kind
Incorrect
-compact-warnings only shortens warning text; it has nothing to do with approval.
DReplace -json with -no-color, the machine-readable mode for plan and apply
Incorrect
-no-color only removes terminal formatting; -json is the machine-readable mode.
Because -json disables prompts, apply needs another source of approval: -auto-approve, or better, a saved plan that was reviewed before the apply job ran.
An instance resource has a provisioner with when = destroy that removes the host from a service registry. Last week the team added lifecycle { create_before_destroy = true } so replacements cause no outage. Since then, replaced hosts stay registered, and the apply logs show no error. What is the cause?
ADestroy-time provisioners need on_failure = continue, or Terraform skips them during replacement
BEnabling create_before_destroy on the resource prevents its destroy-time provisioners from running
CDestroy-time provisioners run only for terraform destroy, never when create_before_destroy replaces a resource
DThe provisioner refers to self, which is unknown during destroy and makes Terraform skip it
Show the answer and why
ADestroy-time provisioners need on_failure = continue, or Terraform skips them during replacement
Incorrect
Nothing in the provisioner's own settings is missing; the documented cause is the lifecycle setting added last week.
BEnabling create_before_destroy on the resource prevents its destroy-time provisioners from running
Correct
HashiCorp documents that setting create_before_destroy prevents destroy-time provisioners from running, so the deregistration never happens.
CDestroy-time provisioners run only for terraform destroy, never when create_before_destroy replaces a resource
Incorrect
They run before the resource is destroyed, which includes the destroy half of a replacement, unless something prevents them.
DThe provisioner refers to self, which is unknown during destroy and makes Terraform skip it
Incorrect
self is how a provisioner refers to its parent resource and is valid in destroy-time provisioners.
Destroy-time provisioners are skipped when create_before_destroy is set and when the resource is tainted. Deregistration that must always run belongs in a tool outside the resource lifecycle, such as the service's own health checks.
A local-exec provisioner runs command = "./notify.sh ${var.branch_name}", and branch names come from pull requests that anyone can open. Security asks the team to remove the risk of shell injection. What does the resource block reference recommend?
AWrap ${var.branch_name} in quotes inside the command string
BPass the value through the environment argument and read it as an environment variable in the command
CSet quiet = true so that Terraform does not print the command
DMove the command into a remote-exec provisioner on the instance
Show the answer and why
AWrap ${var.branch_name} in quotes inside the command string
Incorrect
The value is still interpolated into a shell command; the reference recommends environment variable substitution instead.
BPass the value through the environment argument and read it as an environment variable in the command
Correct
Terraform runs the command in a local shell, and the reference recommends the environment parameter over Terraform variables in the command.
CSet quiet = true so that Terraform does not print the command
Incorrect
quiet only suppresses printing the command; it changes nothing about what runs.
DMove the command into a remote-exec provisioner on the instance
Incorrect
remote-exec runs on the remote machine, and Terraform does not sanitize its interpolated inputs either.
Terraform does not sanitize the code in local-exec or remote-exec provisioners or the values interpolated into it. HashiCorp recommends configuration management tools over provisioners where possible.
An engineer starts terraform apply from the CLI against an HCP Terraform workspace, confirms, and presses Ctrl-C after the apply has begun. What happens to the run?
AHCP Terraform rolls back every change the apply has made so far
BThe apply stops at once, leaving the state locked
CThe state is copied back to the local directory
DLog streaming stops, and the apply keeps running remotely
Show the answer and why
AHCP Terraform rolls back every change the apply has made so far
Incorrect
Terraform does not roll back partially completed applies.
BThe apply stops at once, leaving the state locked
Incorrect
Ctrl-C does not stop an apply that has already started remotely.
CThe state is copied back to the local directory
Incorrect
Remote runs keep state in the workspace.
DLog streaming stops, and the apply keeps running remotely
Correct
Ctrl-C cancels a remote apply only while it is still pending; once it has started it only stops streaming the logs.
Follow or cancel a started remote run from the run page in the HCP Terraform UI.