init, plan, apply & destroy in Depth
The four commands from Install & Your First Apply have options that matter as soon as more than one person, or a pipeline, changes the same infrastructure.
| Command | Does | Safe to run any time? |
|---|---|---|
init |
install providers and modules, configure the backend | yes |
validate |
check syntax, references and types (no API calls, no state) | yes |
plan |
refresh state from the real world, compute the diff | yes, it changes nothing |
apply |
plan (or take a saved plan) and execute it | no |
destroy |
apply of a plan that deletes everything |
no |
show |
print the state, or a saved plan file | yes |
Saved plans: review exactly what will be applied
apply without a plan file computes a new plan, which may differ from the one someone reviewed if the code or the world changed in between. A pipeline therefore saves the plan with -out, shows it for review, and later applies that file. Applying a saved plan doesn't ask for confirmation: the review was the confirmation.
mkdir w && cd w
cat > main.tf <<'HCL'
variable "replicas" {
type = number
default = 2
}
resource "local_file" "app" {
filename = "app.env"
content = "REPLICAS=${var.replicas}\n"
}
resource "terraform_data" "deploy" {
input = var.replicas
}
HCL
tofu init >/dev/null
tofu plan -out=tfplan -detailed-exitcode >/dev/null; echo "plan exit=$?"
echo "--- the saved plan as data"
tofu show -json tfplan | jq -r '.resource_changes[] | "\(.change.actions | join(",")) \(.address)"'
tofu apply tfplan | tail -1
tofu plan -detailed-exitcode >/dev/null; echo "plan exit=$?"
echo "--- a plan made before someone else applied"
tofu plan -var replicas=3 -out=stale >/dev/null
tofu apply -auto-approve -var replicas=4 >/dev/null
tofu apply stale; echo "exit=$?"plan exit=2
--- the saved plan as data
create local_file.app
create terraform_data.deploy
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
plan exit=0
--- a plan made before someone else applied
Error: Saved plan is stale
The given plan file can no longer be applied because the state was changed by
another operation after the plan was created.
exit=1
-detailed-exitcodemakesplanexit with0(no changes),1(error) or2(changes present). Drift-detection jobs and "is there anything to apply?" checks rely on it.show -jsonturns a plan into JSON: policy checks (OPA/Conftest, Checkov), cost estimates (Infracost) and pull-request comments all start from it. Eachresource_changes[]entry hasactionssuch as["create"],["update"],["delete","create"](replace).- A stale plan is refused: the state's serial changed after the plan was saved. That protects you from applying a diff computed against an old picture. Re-plan and review again.
Warning: Plan files contain the full configuration and every value, secrets included. Treat them like state: short-lived, access-controlled artifacts, never committed. OpenTofu can encrypt them (State Encryption).
Options for unusual days
mkdir w && cd w
cat > main.tf <<'HCL'
resource "local_file" "app" {
filename = "app.env"
content = "REPLICAS=2\n"
}
resource "terraform_data" "deploy" {
input = "v1"
}
HCL
tofu init >/dev/null && tofu apply -auto-approve >/dev/null
echo "--- -replace: force a new object without a code change"
tofu plan -replace=terraform_data.deploy | grep -E '^ #|^Plan:'
echo "--- -target: limit the plan to one resource (and its dependencies)"
sed -i 's/REPLICAS=2/REPLICAS=5/; s/"v1"/"v2"/' main.tf
tofu plan -target=local_file.app | grep -E '^ #|^Plan:|^Warning'--- -replace: force a new object without a code change
# terraform_data.deploy will be replaced, as requested
Plan: 1 to add, 0 to change, 1 to destroy.
--- -target: limit the plan to one resource (and its dependencies)
# local_file.app must be replaced
Plan: 1 to add, 0 to change, 1 to destroy.
Warning: Resource targeting is in effect
The targeted plan shows only the file. The change to terraform_data.deploy (v1 to v2) is in the code too, but the plan silently leaves it out: after a targeted apply, code and reality disagree until the next full plan.
| Option | Use it for | Don't use it for |
|---|---|---|
-replace=ADDR |
recreating a broken VM or a corrupted object (the old taint command) |
routine changes |
-target=ADDR |
recovering from a failed partial apply; bootstrapping a dependency loop | splitting a big change into pieces: the rest of the plan is silently left out, hence the warning |
-exclude=ADDR (OpenTofu only) |
the opposite of -target: everything except one resource |
the same caution applies |
-refresh-only |
accept changes made outside the code into state (Drift & Refresh) | fixing drift you don't understand |
-refresh=false |
faster plans on huge configurations when you know nothing drifted | pull-request plans |
-lock-timeout=5m |
waiting for a colleague's run instead of failing (Locking) | |
-parallelism=N |
APIs that rate-limit you |
Two things to know about destroy: tofu destroy is the same as tofu apply -destroy, and tofu plan -destroy previews it. In shared environments, protect important resources with prevent_destroy and keep destroy out of pipelines except for short-lived preview environments.
Test yourself
A pipeline saved a plan for review. Which command applies exactly that reviewed plan?
tofu apply -auto-approvetofu apply tfplantofu plan -applytofu apply -target=tfplan
What does tofu plan -detailed-exitcode return when there are changes to apply?
- 0
- 1
- 2
- 3
Why is -target discouraged for everyday changes?
- It is slower
- It leaves the rest of the configuration out of the plan, so code and reality drift apart unnoticed
- It deletes other resources
- It only works with OpenTofu