0
0
Terraformcloud~10 mins

Terraform fmt for formatting - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Terraform fmt for formatting
Write Terraform code
Run 'terraform fmt'
Terraform scans files
Detect formatting issues?
NoExit, 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.