Skip to content

004 · Domain 3: Core Terraform workflow · 13% of the exam

Task 3.7: Apply formatting and style adjustments to a configuration

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.

Study it

  • terraform fmt and the style conventions

    Lesson coming

Sample questions

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?

  1. Aterraform fmt -diff
  2. Bterraform validate -json
  3. Cterraform fmt -list=false
  4. Dterraform fmt -check
Show the answer and why
  • Aterraform fmt -diff

    Incorrect

    -diff shows the formatting changes but still rewrites the files and exits successfully.

  • Bterraform validate -json

    Incorrect

    validate checks syntax and consistency, not formatting.

  • Cterraform fmt -list=false

    Incorrect

    That only stops fmt from listing the files it changes.

  • Dterraform 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?

  1. ARewrites them in canonical format and lists the files it changed
  2. BPrints the problems it finds and leaves the files as they are
  3. CFails the command until the files are fixed by hand
  4. DFormats the files in memory for the next plan only
Show the answer and why
  • ARewrites 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.

  • BPrints the problems it finds and leaves the files as they are

    Incorrect

    By default fmt writes its changes; it does not only report them.

  • CFails the command until the files are fixed by hand

    Incorrect

    fmt fixes formatting itself.

  • DFormats the files in memory for the next plan only

    Incorrect

    fmt writes the changes to the files.

fmt fixes files in place by default.

HashiCorp documentation

Question 3 · choose 1

A developer wants terraform fmt to print the exact changes it makes to each file. Which option shows them?

  1. A-list=false
  2. B-write=true
  3. C-diff
  4. D-no-color
Show 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.

HashiCorp documentation

Question 4 · choose 1

A team upgrades Terraform to a new minor version. What does the fmt documentation recommend doing with existing modules?

  1. ANothing; fmt rules are frozen across versions
  2. BRun terraform validate -fmt to convert them to the new version's style
  3. CDelete the .terraform directory so fmt uses the new rules
  4. DRun terraform fmt on them, as the canonical format can change slightly
Show the answer and why
  • ANothing; fmt rules are frozen across versions

    Incorrect

    New formatting rules can be added in new versions.

  • BRun terraform validate -fmt to convert them to the new version's style

    Incorrect

    There is no such option.

  • CDelete the .terraform directory so fmt uses the new rules

    Incorrect

    fmt rules are built into the binary, not cached in .terraform.

  • DRun 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.)

  1. AIndent with one tab character per nesting level
  2. BIndent two spaces for each nesting level
  3. CAlign the equals signs of consecutive single-line arguments
  4. DPut every argument on its own file
  5. EWrite all block types in uppercase
Show the answer and why
  • AIndent with one tab character per nesting level

    Incorrect

    The convention is two spaces, not tabs.

  • BIndent two spaces for each nesting level

    Correct

    The style guide asks for two-space indentation per nesting level, which fmt applies.

  • CAlign the equals signs of consecutive single-line arguments

    Correct

    Consecutive single-line arguments at the same level have aligned equals signs.

  • DPut every argument on its own file

    Incorrect

    Files group related blocks; there is no one-argument-per-file rule.

  • EWrite 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?

  1. AUse terraform fmt -list=network.tf
  2. BPass the file as the target: terraform fmt network.tf
  3. CUse terraform fmt -target=network.tf
  4. DRename every other file in the directory temporarily, then rename them back
Show the answer and why
  • AUse terraform fmt -list=network.tf

    Incorrect

    -list is a true or false switch for listing changed files, not a file selector.

  • BPass 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.

  • CUse terraform fmt -target=network.tf

    Incorrect

    -target belongs to plan and apply, not fmt.

  • DRename 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.

Practise domain 3 →Practise all domains →