Lesson 4 of 4 Initializing, Validating and Formatting Configuration

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.

OptionEffect
-recursiveAlso format files in subdirectories, such as modules/
-checkChange nothing; exit with a non-zero status if any file needs formatting
-diffShow the formatting changes as a diff
-write=falsePrint formatted output without writing files
-list=falseDo 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.

Why terraform fmt -check fails in CI while a developer's terraform fmt in the root changes nothing. fmt does not recurse by default, so the unformatted file is inside modules/. Running terraform fmt -recursive locally fixes it, and the check passes after the commit.
File names other than modules/ are illustrative.
Scenario questions

Using fmt

Exam skills 3.7.1

Choose the terraform fmt options each situation needs.

Log in to try this activity Interactive activities are free with an account.

Style conventions beyond fmt

HashiCorp's style guide adds conventions that fmt does not enforce. The most important are listed below.

  • Use lowercase snake_case for resource, variable, output and local names, and do not repeat the type in the name: aws_instance.web, not aws_instance.web_instance.
  • Inside a block, place meta-arguments such as count and for_each first, then other arguments, then nested blocks, with lifecycle and depends_on last.
  • Organise files by purpose: terraform.tf or versions.tf for the terraform block, providers.tf, main.tf, variables.tf, outputs.tf and locals.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.
True or false

Style conventions

Exam skills 3.7.2

Check HashiCorp's style conventions that terraform fmt does not enforce.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

Check yourself

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.