Cheat Sheet 3: Core Workflow
Workflow: Write → (fmt, init, validate) → Plan → review → Apply → … → Destroy
terraform init: sets up the backend, downloads modules (.terraform/modules) and providers (.terraform/providers), and writes the lock file. Safe to re-run, and never touches infrastructure.
- Re-run after adding or changing a provider, module source or version, or backend or
cloudblock. - Flags:
-upgrade·-backend=false·-backend-config·-migrate-state·-reconfigure·-input=false
terraform validate: checks syntax, schema, references, and types. It requires init, makes no API calls, and doesn't use tfvars values. Use -json for CI.
terraform plan: refreshes, then diffs. Changes nothing, and refreshed values aren't saved.
| Flag | Purpose |
|---|---|
-out=f |
Save the plan. Read it with terraform show f. Treat it as sensitive. |
-refresh-only |
Propose state-only updates to match reality |
-refresh=false |
Skip refresh |
-target=addr |
Limit scope, with a warning. Exceptional use only. |
-replace=addr |
Force replacement. Replaces the deprecated taint. |
-destroy |
Preview destruction |
-var / -var-file |
Supply variable values |
-detailed-exitcode |
0 = no changes · 1 = error · 2 = changes |
Plan symbols: + create · - destroy · ~ update · -/+ replace (destroy first) · +/- replace (create first) · <= read data · # forces replacement
terraform apply
- No plan file: plans and prompts.
-auto-approveskips the prompt. apply planfile: applies exactly, no prompt. Fails if the plan is stale.- No rollback on failure. Completed resources stay in state. Failed creations become tainted and are replaced next run.
-refresh-onlywrites state only.
Destroy
terraform destroy=terraform apply -destroy.plan -destroy= preview only.- Runs in reverse dependency order.
prevent_destroymakes the plan error. - To remove one resource, delete its block and run
apply(preferred), ordestroy -target(exceptional).
terraform fmt: whitespace only, current directory only by default.
-recursivesubdirectories ·-checkCI gate (non-zero exit, no writes) ·-diffshow changes ·-write=falseno writes- It isn't a validator.
Only apply and destroy change real infrastructure. Only apply, apply -refresh-only, destroy, and state commands write state.
Module 3 self-check
Module 3 self-check.
- Objectives 3a (Q1), 3b (Q2), 3c (Q3), 3d (Q4, Q6, Q7), 3e (Q4, Q5, Q8), 3f (Q9), and 3g (Q10) are each covered by a lesson and lab steps.
- Lab HCL uses valid
kreuzwerker/dockerv3 arguments (image_id,keep_locally, and aportsblock). The formattedvariables.tfoutput matchesfmt. - Error messages are paraphrased where exact wording varies by patch release.
- Every answer is rechecked against documented 1.12 CLI behavior: saved-plan staleness, no rollback,
taintdeprecation, andfmt -checkexit behavior.