Infrastructure Lifecycle and Operations · Topic 2 of 5
Planning and Applying Changes
Terraform Professional 002: planning modes, reading a plan, saved plans, partial apply failures, state locks, force-unlock and provisioner behavior.
- 5 min
-refresh-onlytouches state and outputs, never real resources.-/+destroys first;+/-creates first.- A saved plan applies without a prompt and takes no new planning options.
What it is
terraform plan compares configuration, state and real infrastructure, then proposes actions. terraform apply carries out those actions and records the results in state.
At this level the exam goes past the commands. It asks you to predict a plan, choose the right mode for a situation, and say what remains after an apply fails halfway.
How it works
Modes and options
| Option | Effect | Use it when |
|---|---|---|
-refresh-only | Plans only to update state and root outputs to match real objects | You want to review or accept drift |
-destroy | Plans to remove everything in the state | Tearing down an environment |
-replace=ADDRESS | Plans to replace one resource instance; repeatable | An object is degraded but its configuration is unchanged |
-target=ADDRESS | Limits the plan to matching instances and their dependencies | Exceptional recovery only |
-refresh=false | Skips syncing with real objects | Faster plan, but outside changes may be missed |
-out=FILE | Saves the plan for a later apply | Plan and apply are separate steps |
-parallelism=n | Limits concurrent operations; the default is 10 | An API is rate limiting you |
-replace cannot be combined with -destroy, and -refresh=false cannot be used in refresh-only mode.
Reading a plan
| Symbol | Meaning |
|---|---|
+ | Create |
~ | Update in place |
- | Destroy |
-/+ | Destroy, then create the replacement |
+/- | Create the replacement, then destroy (create_before_destroy) |
<= | Read a data source |
An attribute marked # forces replacement is why an update has become a replacement. Changing it on one instance produces a summary of Plan: 1 to add, 0 to change, 1 to destroy, which means an outage risk rather than a small edit.
Saved plans
terraform plan -input=false -out=tfplan
terraform show tfplan
terraform apply -input=false tfplan
Applying a saved plan does not prompt, because passing the file counts as approval. You cannot add planning options at that point, since the decisions are already in the file.
If another operation changed the state after the plan was made, Terraform rejects the file as stale and you must plan again. Saved plans hold sensitive values in cleartext, so protect them like secrets.
When an apply fails partway
Terraform does not roll back. On an error it logs the failure, saves the changes that completed, releases the lock and exits.
Trace an apply that creates A, B and C, where C fails on an API quota:
- A and B exist and are recorded in state.
- C is not created, and the command exits with an error.
- You fix the cause and run apply again.
- The new plan proposes only C, because A and B already match.
Locks
Locking is automatic on any operation that could write state, on backends that support it. A second apply against a locked state fails with Error acquiring the state lock; it does not queue.
terraform apply -lock-timeout=5m
terraform force-unlock LOCK_ID
-lock-timeout retries for the given time. force-unlock removes a lock left behind when automatic unlocking failed, such as after a crashed runner.
It changes no infrastructure, but use it only on your own lock. Unlocking someone else's active run allows two writers. -lock=false is riskier still and is not recommended.
Provisioners
Provisioners are a last resort because Terraform cannot model what they do.
| Situation | Result |
|---|---|
| Creation-time provisioner fails | Resource is marked tainted and replaced on the next apply |
Same failure with on_failure = continue | The error is ignored and the apply continues |
when = destroy provisioner fails | Apply errors, and it reruns on the next apply |
| Resource is tainted | Destroy-time provisioners do not run |
How the exam asks about this
- "An instance is unhealthy, the configuration is correct, and you need a new one." Run terraform apply -replace=ADDRESS, which replaces
taint. - "An apply failed on the fifth of eight resources; what happens to the first four?" They remain and stay in state; there is no rollback.
- "A CI runner crashed and every later run reports a lock error." Confirm no run is active, then run terraform force-unlock LOCK_ID.
What to remember for the exam
-refresh-onlytouches state and outputs, never real resources.-/+destroys first;+/-creates first.- A saved plan applies without a prompt and takes no new planning options.
- A failed apply keeps completed work and does not roll back.
- A held lock causes an error unless
-lock-timeoutis set. - A failed creation-time provisioner taints the resource by default.
-auto-approve also approves destructive actions. Reserve it for non-critical environments, or apply a saved plan that someone has already reviewed.
Common mistakes
Expecting a rollback. Options that say Terraform restores the previous state after a failure are wrong. The fix is to correct the cause and apply forward.
Using -target as routine practice. It also pulls in everything the target depends on, and regular use can leave drift undetected. Split the configuration instead.
Reading only the summary line. One replacement looks small in the counts, yet it can mean a database recreated. Find the # forces replacement line before approving.
Check yourself
What does Terraform do with completed resources when an apply fails partway?
It keeps them and records them in state, releases the lock and exits with an error. The next apply handles only what remains.
Can you add -replace to terraform apply along with a saved plan file?
No. A saved plan already contains its final decisions, so no additional planning modes or options can be added.
What happens to a resource when its creation-time provisioner fails with default settings?
Terraform marks it tainted, and the next apply destroys and recreates it.
Topic 2 of 5 in Infrastructure Lifecycle and Operations
Test yourself on Infrastructure Lifecycle and Operations
The full HashiCorp Terraform Infrastructure Engineer Professional question bank has 600 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by HashiCorp. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.