TF-Associate-004 - Core Terraform workflow - 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

Free 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. 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.

See more TF-Associate-004 practice questions, answers explained.

More in this domain

Back to all Core Terraform workflow objectives, or the TF-Associate-004 cert hub.

Examworthy is not affiliated with or endorsed by HashiCorp. Original, blueprint-aligned practice material only.