Formatting and Style Conventions
terraform fmt rewrites configuration into a canonical layout. This lesson covers fmt and its options and the wider style conventions for Terraform code.
Learning outcomes
After completing this lesson, you will be able to:
- Describe what terraform fmt changes and its main options.
- Identify Terraform style conventions that fmt does not enforce.
What fmt does
terraform fmt rewrites Terraform files into the canonical format, so that all configurations look the same and diffs show only meaningful changes. It indents nested content by two spaces, aligns the equals signs of consecutive single-line arguments and normalises spacing. The following example shows a block before and after formatting.
# Before
resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id
instance_type="t3.micro"
tags={Name="web"}
}
# After terraform fmt
resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
tags = { Name = "web" }
}fmt processes .tf, .tfvars and .tftest.hcl files. It changes layout only, never meaning, and it does not check that the configuration is valid beyond being parseable. It prints the name of each file it changed.
Options
By default, fmt formats files in the current directory only. The following options adapt it for repositories and pipelines.
| Option | Effect |
|---|---|
| -recursive | Also format files in subdirectories, such as modules/ |
| -check | Change nothing; exit with a non-zero status if any file needs formatting |
| -diff | Show the formatting changes as a diff |
| -write=false | Print formatted output without writing files |
| -list=false | Do not list the files that were changed |
terraform fmt -check -recursive is a standard CI gate: it fails the build when someone commits unformatted code, and the fix is to run terraform fmt -recursive locally.
The following diagram shows why fmt without -recursive misses files in subdirectories.

Using fmt
Style conventions beyond fmt
HashiCorp's style guide adds conventions that fmt does not enforce. The most important are listed below.
- Use lowercase
snake_casefor resource, variable, output and local names, and do not repeat the type in the name:aws_instance.web, notaws_instance.web_instance. - Inside a block, place meta-arguments such as
countandfor_eachfirst, then other arguments, then nested blocks, withlifecycleanddepends_onlast. - Organise files by purpose:
terraform.tforversions.tffor the terraform block,providers.tf,main.tf,variables.tf,outputs.tfandlocals.tf. - Give every variable a type and description, and every output a description.
- Use
#for comments, and comment the reason for unusual choices rather than restating the code.
Style conventions
A failing CI check
A pipeline step runs terraform fmt -check and fails, but a developer runs terraform fmt in the repository root and sees no changes. The repository has a modules/ directory.
The decisive fact is that fmt does not recurse by default.
The unformatted file is probably inside modules/. If the pipeline runs fmt with -recursive, or runs it in each module directory, the check finds it, while the developer's plain terraform fmt only looked at the root. Running terraform fmt -recursive locally fixes the files, and the check passes after the commit.
Which terraform fmt option makes a CI job fail when files are not formatted, without changing them?
Show answer
-check, which exits with a non-zero status if any file needs formatting.
Exam Tips
Remember for the exam
- fmt fixes layout only: two-space indentation and aligned equals signs.
- -check for CI, -recursive for subdirectories, -diff to see changes.
- Naming, ordering and file layout are conventions you follow yourself.