TF-Associate-004 - Core Terraform workflow (19% of the exam) - Section 3g
Apply formatting and style adjustments to a configuration.
terraform fmt rewrites configuration to the canonical style (indentation, alignment) and is safe to run repeatably; -check reports without writing. Candidates should know fmt changes formatting only, never behaviour, and pairs well with CI.
terraform fmtcanonical formattingfmt -checkformatting does not change behaviour
Practice question for this objective
lock_openFree sampleCore Terraform workfloweasy
A CI pipeline needs a step that fails the build when configuration files are not in canonical style, but that must not modify any files on disk. Which terraform fmt invocation fits this requirement?
terraform fmt <flag>
Aterraform fmt -write=true, which reformats the files and then reports which ones it changed.
Bterraform fmt -recursive, which walks subdirectories and formats every configuration file it finds.
Cterraform fmt -check, which reports whether files conform and returns a non-zero exit code without writing changes.check_circle Correct
Dterraform validate, which confirms the configuration is well formed and consistent before the pipeline continues.
Know that terraform fmt -check reports formatting compliance and exits non-zero without writing changes, suiting CI checks. The -check flag turns terraform fmt into a read-only compliance test. It lists files that are not in canonical style and returns a non-zero exit status so automation can fail the build, but crucially it does not rewrite any file, which is exactly what a verification step needs.
Why A is wrong: Tempting because it produces a list of affected files, but -write=true actually rewrites them on disk, which the pipeline must not do.
Why B is wrong: Plausible because CI often wants to cover nested modules, but -recursive still writes changes to disk and does not by itself provide a non-writing check.
Why C is correct: Correct. The -check flag makes fmt report non-conforming files and exit non-zero for CI to fail on, while leaving the files on disk unmodified.
Why D is wrong: Wrong because validate checks configuration correctness, not formatting style, so it will not detect files that merely deviate from the canonical layout.
Answers that look right on this material and are not. Each one is a distractor from a different question in the TF-Associate-004 bank for this domain.
Formatting can change behaviour, so fmt should always be followed by a fresh plan to confirm no resources were altered.
errorWhy it is wrong: Tempting because running a plan after changes is generally prudent, but the premise is false: fmt cannot alter behaviour, so the caution is based on a misconception.
terraform validate
errorWhy it is wrong: validate checks that the configuration is syntactically valid and internally consistent, but it does not rewrite files or adjust indentation and spacing, so the style stays inconsistent.
It can change resource behaviour because it may reorder arguments that Terraform evaluates in sequence.
errorWhy it is wrong: Terraform does not evaluate top-level resource arguments in source order, and fmt does not reorder them in a way that changes meaning, so this fear is misplaced.