HashiCorp Terraform Associate (004) · Section 3 · 12% of the exam
Generate and review an execution plan for Terraform
One of the objectives of the HashiCorp Certified: Terraform Associate (004) exam, in the section “Core Terraform workflow”. Here is what it covers, the points to remember and a question to try.
What it covers
Validate catches configuration mistakes without contacting any provider. Plan refreshes state and shows exactly what would change, and apply carries the plan out. This lesson covers how to read plan output, the planning modes and options, saved plans, and what happens when an apply fails.
Points to remember
validatechecks syntax and internal consistency, needs init (-backend=falseis enough) and never contacts providers or backends.planrefreshes state, compares it with the configuration and proposes changes without applying them.- Plan symbols:
+create,-destroy,~update in place,-/+replace (destroy then create),+/-replace (create then destroy),<=read. - Planning modes: normal,
-destroy,-refresh-only.-replacerecreates one resource;-targetis for exceptional cases. plan -outsaves a plan;apply FILEruns it without a prompt and rejects it if state changed (stale plan).
Try a question
A CI job must distinguish three cases: no changes, an error, and pending changes. Which option of terraform plan provides different exit codes for these?
- A
-compact-warnings, where 0 means no warnings and 2 means many warnings were suppressed - B
-detailed-exitcode: 0 no changes, 1 error, 2 changes present - C
-lock=false, where the exit code shows whether the state lock was acquired - D
-json, where 2 means the JSON output was invalid and 1 that none was produced
Show the answer
B. By default plan exits 0 on success even if changes are pending. -detailed-exitcode returns 2 when the plan succeeded and contains changes.
Written for practice in the style of the exam: never a question from the real exam.
In the course
The lesson Validate, plan and apply teaches this objective (about 45 minutes), and 21 practice questions test it. The course has lessons, exam-style questions, timed practice exams and an AI tutor for every objective, and a pass prediction that tells you when you're ready to book.