Certifications / Terraform Associate 004 · Domain 3: Core Terraform workflow · 13% of the exam
terraform fmt: the canonical style, checking it in CI with -check, subdirectories with -recursive, which files it touches, and the style conventions it does not enforce.
Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.
Question 1 · choose 1
A team wants its pull request checks to fail when any .tf file is not in canonical format, without the check rewriting files in the build workspace. Which command should the pipeline run? A terraform fmt -diffB terraform validate -jsonC terraform fmt -list=falseD terraform fmt -checkShow the answer and why A terraform fmt -diff
Incorrect
-diff shows the formatting changes but still rewrites the files and exits successfully.
B terraform validate -json
Incorrect
validate checks syntax and consistency, not formatting.
C terraform fmt -list=false
Incorrect
That only stops fmt from listing the files it changes.
D terraform fmt -check
Correct
-check exits with a non-zero status when input is not formatted and implies -write=false, so files are not modified.
fmt -check is the CI form of fmt: report and fail, never write.
Question 2 · choose 1
What does terraform fmt do by default when run in a directory with badly indented files? A Rewrites them in canonical format and lists the files it changedB Prints the problems it finds and leaves the files as they areC Fails the command until the files are fixed by handD Formats the files in memory for the next plan onlyShow the answer and why A Rewrites them in canonical format and lists the files it changed
Correct
fmt overwrites files to match the canonical format and lists the files with inconsistencies, unless -list=false or -write=false is set.
B Prints the problems it finds and leaves the files as they are
Incorrect
By default fmt writes its changes; it does not only report them.
C Fails the command until the files are fixed by hand
Incorrect
fmt fixes formatting itself.
D Formats the files in memory for the next plan only
Incorrect
fmt writes the changes to the files.
fmt fixes files in place by default.
Question 3 · choose 1
A developer wants terraform fmt to print the exact changes it makes to each file. Which option shows them? A -list=falseB -write=trueC -diffD -no-colorShow the answer and why A -list=false
Incorrect
That hides the list of changed files.
B -write=true
Incorrect
That is the default, which rewrites files without showing the changes.
C -diff
Correct
-diff displays the diffs of formatting changes.
D -no-color
Incorrect
That removes color from output.
-diff shows what fmt changes.
Question 4 · choose 1
A team upgrades Terraform to a new minor version. What does the fmt documentation recommend doing with existing modules? A Nothing; fmt rules are frozen across versionsB Run terraform validate -fmt to convert them to the new version's styleC Delete the .terraform directory so fmt uses the new rulesD Run terraform fmt on them, as the canonical format can change slightlyShow the answer and why A Nothing; fmt rules are frozen across versions
Incorrect
New formatting rules can be added in new versions.
B Run terraform validate -fmt to convert them to the new version's style
Incorrect
There is no such option.
C Delete the .terraform directory so fmt uses the new rules
Incorrect
fmt rules are built into the binary, not cached in .terraform.
D Run terraform fmt on them, as the canonical format can change slightly
Correct
The canonical format may change in minor ways between versions, so the docs recommend running fmt after upgrading.
Formatting rules travel with the Terraform binary; re-run fmt after an upgrade.
Question 5 · choose 2
Which two layout conventions does the Terraform style guide describe, and does terraform fmt apply them? (Choose TWO.) A Indent with one tab character per nesting levelB Indent two spaces for each nesting levelC Align the equals signs of consecutive single-line argumentsD Put every argument on its own fileE Write all block types in uppercaseShow the answer and why A Indent with one tab character per nesting level
Incorrect
The convention is two spaces, not tabs.
B Indent two spaces for each nesting level
Correct
The style guide asks for two-space indentation per nesting level, which fmt applies.
C Align the equals signs of consecutive single-line arguments
Correct
Consecutive single-line arguments at the same level have aligned equals signs.
D Put every argument on its own file
Incorrect
Files group related blocks; there is no one-argument-per-file rule.
E Write all block types in uppercase
Incorrect
Block types are lowercase keywords.
Two spaces and aligned equals signs: the two layout rules readers notice most.
Question 6 · choose 1
A developer wants terraform fmt to format only one file, network.tf, in a directory of many files. How is this done? A Use terraform fmt -list=network.tfB Pass the file as the target: terraform fmt network.tfC Use terraform fmt -target=network.tfD Rename every other file in the directory temporarily, then rename them backShow the answer and why A Use terraform fmt -list=network.tf
Incorrect
-list is a true or false switch for listing changed files, not a file selector.
B Pass the file as the target: terraform fmt network.tf
Correct
fmt accepts targets, such as a file or directory, instead of scanning the current directory.
C Use terraform fmt -target=network.tf
Incorrect
-target belongs to plan and apply, not fmt.
D Rename every other file in the directory temporarily, then rename them back
Incorrect
Targets make that unnecessary.
fmt formats the current directory by default, or exactly the targets you give it.
Practice domain 3 → Practice all domains →
← Task 3.6 Task 4.1 →