Developer and Text Tools

Terraform fmt Explained: Alignment Rules, Validation and Module Trees

What terraform fmt changes in .tf files, why some arguments are not aligned, and how to check variables, outputs, references and modules before you plan.

Saizul Amin
Saizul Amin7 Nov 2026 · 3 min read
Terraform fmt Explained: Alignment Rules, Validation and Module Trees

Terraform has an official formatter, and most teams run it in CI so that every .tf file has the same layout. Even so, its output can be puzzling at first: why are some equals signs aligned and others not, and why did a string lose its quotes? This guide explains the rules and shows how the Terraform HCL Formatter and Validator applies them in a browser, together with checks you would otherwise only see when you run plan.

Indentation and spacing

terraform fmt indents every nested block and every multi-line list or object by two spaces. Each argument has exactly one space before and after its equals sign, function calls have no space before the parenthesis, list items are separated by a comma and a space, and an object written on one line has a space inside its braces. Empty blocks are written as {} with no space between the braces.

The alignment rule

The detail that confuses most people is alignment. fmt lines up the equals signs of consecutive arguments in the same block, padding the shorter names. A group ends at a blank line, a comment line, a nested block, or an argument whose value continues on the next lines. So in a resource with ami, instance_type and count followed by tags = {, the first three are aligned with each other, but tags keeps a single space, and the keys inside the tags object form their own group.

Trailing comments are aligned in the same way, so a column of notes after a set of arguments stays readable.

Fixups beyond spacing

Recent versions of terraform fmt also tidy two leftovers from Terraform 0.11. A string that contains nothing but one interpolation, like "${var.ami_id}", is unwrapped to var.ami_id. And a legacy quoted type in a variable block, such as type = "string" or type = "list", becomes string or list(string). Both are options in the tool, on by default, and each change is listed so you can see it.

Heredocs, template directives and comments are never changed.

Validation without a backend

terraform validate needs initialised providers, which means network access and credentials. Many mistakes, though, can be found from the files alone, and the tool checks those. It reports unknown top-level block types, resource and data blocks with the wrong number of labels, the same argument set twice in one block, duplicate resource names, outputs without a value and modules without a source.

It also follows references. var.name, local.name, module.name, data.type.name and resource addresses like aws_instance.web are compared with the declarations in the same folder. A reference to something that is not declared is a warning rather than an error, because you may simply not have loaded the file that declares it.

Variables, outputs and tfvars

The project tab lists every variable with its type, default, description and sensitivity, where it is declared and how many times it is used. Variables nobody uses and variables without a type or description get a note. Outputs are listed with their value and description. A .tfvars file is checked against the variables, so a value for a variable that does not exist, often a typo, is reported before plan ignores it with only a warning.

Seeing the module tree

Open a whole folder rather than single files and the paths are kept. Each module block with a local source such as ./modules/network is matched to the loaded files, and the tree shows the nesting, for example root, then module.network, then any modules it calls. Registry and Git sources are shown with their version so you can spot an unpinned module at a glance.

A routine that catches problems early

Before opening a pull request, open the changed folder, check that the diff only contains the formatting you expect, read the warnings and fix any undeclared references, then apply the formatting and download the files. Your configuration and tfvars values never leave the browser, which matters when they include account IDs or other details you would rather not paste into an online service. Keep terraform fmt -check and terraform validate in CI as the final word.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages