Bird
Raised Fist0
Terraformcloud~10 mins

Terraform fmt for formatting - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Terraform fmt for formatting
Write Terraform code
↓
Run 'terraform fmt'
↓
Terraform scans files
↓
Detect formatting issues?
No→Exit, files unchanged
Yes↓
Rewrite files with standard format
↓
Exit, files formatted
Terraform fmt checks your code files and rewrites them to follow standard formatting rules, making your code neat and consistent.
Execution Sample
Terraform
terraform fmt
Runs the formatter on Terraform files in the current directory to fix indentation and spacing.
Process Table
StepActionFiles CheckedFormatting Issues FoundFiles Changed
1Start terraform fmtmain.tf, variables.tfNoNo
2Check main.tfmain.tfYesYes
3Rewrite main.tf with standard formatmain.tfN/AYes
4Check variables.tfvariables.tfNoNo
5Finish formattingmain.tf, variables.tfN/AYes
💡 All files checked; main.tf reformatted; variables.tf unchanged.
Status Tracker
VariableStartAfter Step 2After Step 3Final
main.tf contentUnformatted codeUnformatted codeFormatted codeFormatted code
variables.tf contentFormatted codeFormatted codeFormatted codeFormatted code
Key Moments - 2 Insights
Why does terraform fmt sometimes not change any files?
If the files are already formatted correctly, terraform fmt finds no issues and leaves them unchanged, as shown in step 1 and 4 of the execution table.
Does terraform fmt change the code logic or only formatting?
Terraform fmt only changes spacing and indentation, not the code logic or content, as seen in the variable_tracker where content changes only reflect formatting.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does terraform fmt rewrite a file?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Check the 'Action' and 'Files Changed' columns in the execution table.
According to the variable tracker, what is the state of variables.tf after formatting?
ADeleted file
BUnformatted code
CFormatted code
DPartially formatted code
💡 Hint
Look at the 'variables.tf content' row in the variable_tracker after final step.
If main.tf was already formatted, how would the execution table change?
AStep 3 would be skipped with no files changed
BStep 2 would show formatting issues found
CStep 4 would rewrite variables.tf
DStep 5 would show files deleted
💡 Hint
Refer to the 'Formatting Issues Found' and 'Files Changed' columns in the execution table.
Concept Snapshot
terraform fmt
- Command to format Terraform files
- Fixes indentation and spacing
- Does not change code logic
- Runs in current directory by default
- Keeps code neat and consistent
Full Transcript
Terraform fmt is a command that checks your Terraform code files for formatting issues like indentation and spacing. When you run terraform fmt, it scans all Terraform files in the current folder. If it finds files that are not formatted properly, it rewrites them to follow the standard style. If files are already formatted, it leaves them unchanged. This helps keep your code clean and easy to read without changing how it works.

Practice

(1/5)
1. What is the main purpose of the terraform fmt command?
easy
A. To check for syntax errors in Terraform files
B. To deploy infrastructure to the cloud
C. To automatically format Terraform configuration files for readability
D. To destroy all Terraform-managed resources

Solution

  1. Step 1: Understand the role of terraform fmt

    This command is designed to clean up and standardize the formatting of Terraform files, making them easier to read and maintain.
  2. Step 2: Differentiate from other commands

    Unlike commands that deploy or destroy resources, terraform fmt only changes the file layout without affecting infrastructure.
  3. Final Answer:

    To automatically format Terraform configuration files for readability -> Option C
  4. Quick Check:

    Formatting = Readability [OK]
Hint: Remember: fmt = format code, not deploy or destroy [OK]
Common Mistakes:
  • Confusing fmt with terraform apply
  • Thinking fmt checks syntax errors
  • Assuming fmt manages resources
2. Which of the following is the correct way to run terraform fmt on all files in the current directory?
easy
A. terraform fmt
B. terraform fmt -recursive
C. terraform fmt -all
D. terraform fmt --format-all

Solution

  1. Step 1: Recall default behavior of terraform fmt

    By default, running terraform fmt without flags formats all Terraform files in the current directory.
  2. Step 2: Check other options

    Options like -recursive exist to format subdirectories, but -all and --format-all are invalid flags.
  3. Final Answer:

    terraform fmt -> Option A
  4. Quick Check:

    Default fmt formats current directory [OK]
Hint: No flags needed to format current folder files [OK]
Common Mistakes:
  • Using invalid flags like -all or --format-all
  • Confusing recursive flag with default behavior
  • Expecting fmt to format files outside current directory by default
3. Given this Terraform snippet before formatting:
resource "aws_instance" "example" {
ami = "ami-123456"
instance_type= "t2.micro"
}

What will terraform fmt do to this code?
medium
A. Add comments explaining each line
B. Add spaces around the equals sign and indent the block properly
C. Remove the resource block entirely
D. Change the resource type to "aws_server"

Solution

  1. Step 1: Identify formatting issues

    The snippet lacks spaces around the equals sign and proper indentation inside the resource block.
  2. Step 2: Understand what terraform fmt fixes

    It adds spaces around operators and indents blocks correctly but does not change resource types or add comments.
  3. Final Answer:

    Add spaces around the equals sign and indent the block properly -> Option B
  4. Quick Check:

    Fmt fixes spacing and indentation [OK]
Hint: Fmt fixes spacing and indentation, not content [OK]
Common Mistakes:
  • Thinking fmt changes resource types
  • Expecting fmt to add comments
  • Assuming fmt deletes code blocks
4. You ran terraform fmt but your Terraform files still look messy. What is the most likely reason?
medium
A. Terraform fmt does not change file formatting
B. Terraform fmt only works on .tfvars files
C. Your files have syntax errors preventing formatting
D. You ran terraform fmt in the wrong directory

Solution

  1. Step 1: Check where terraform fmt runs

    The command formats files in the current directory by default, so running it elsewhere won't affect your target files.
  2. Step 2: Eliminate other options

    Terraform fmt does change formatting, works on .tf files, and does not require syntax correctness to format.
  3. Final Answer:

    You ran terraform fmt in the wrong directory -> Option D
  4. Quick Check:

    Run fmt where files are located [OK]
Hint: Run fmt in folder with your Terraform files [OK]
Common Mistakes:
  • Assuming fmt fixes syntax errors
  • Thinking fmt works on .tfvars files only
  • Expecting fmt to format files outside current folder
5. You want to ensure all Terraform files in your project and all subfolders are formatted consistently before committing code. Which command should you use?
hard
A. terraform fmt -recursive
B. terraform fmt -all
C. terraform fmt --check
D. terraform fmt --init

Solution

  1. Step 1: Understand recursive formatting

    The -recursive flag tells terraform fmt to format files in the current directory and all subdirectories.
  2. Step 2: Review other options

    -all and --init are invalid flags; --check only verifies formatting without changing files.
  3. Final Answer:

    terraform fmt -recursive -> Option A
  4. Quick Check:

    Recursive flag formats all folders [OK]
Hint: Use -recursive to format all subfolders [OK]
Common Mistakes:
  • Using invalid flags like -all
  • Confusing --check with formatting action
  • Trying to use --init for formatting