b2KIT

Terraform / HCL Formatter

Format HashiCorp Configuration Language (HCL) and Terraform files with canonical style.

Tested tool guide Tested browser tools Checked August 16, 2026

What Terraform / HCL Formatter does, with a checked example

Paste an HCL or Terraform configuration and this tool returns the same file rewritten in canonical style: two-space indentation, aligned equals signs, and braces positioned per the canonical format defined in the HCL specification. The pass is purely about layout - comments, string values, and the order of arguments and blocks survive unchanged; input that does not parse is rejected outright; and the text never leaves the browser. The thing users most often misread: clean formatting is not validation. The output can look flawless and still reference undefined variables or misspelled argument names.

Worked example

A concrete input and expected output from the current implementation.

Input

resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = var.instance_type
tags = {
Name = "web"
Env = "prod"
}
}

Expected output

resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = var.instance_type
  tags         = {
    Name = "web"
    Env  = "prod"
  }
}

The body lines are indented two spaces, and the equals signs of ami, instance_type, and tags all align to the longest key, instance_type. Inside the object, Name and Env align to Name, and each closing brace drops to the indentation of the construct it closes.

How the result is produced

1

What gets rewritten

Formatting is applied to the document as a whole rather than line by line: two spaces of indentation per nesting level, equals signs aligned across consecutive assignment lines to the longest key in the run, and the same alignment applied to keys inside object literals. An opening brace stays on the block header line and the closing brace moves to its own line at the parent indentation.

2

What is left alone

The formatter never edits the content of values: quote style, escape sequences, and the literal text of strings pass through unchanged, and the order of arguments, blocks, and object keys is preserved exactly. A blank line splits an alignment run, so spacing choices affect what aligns with what. An object literal written on one line stays on one line, and comments stay where they are.

Good uses

  • Cleaning up a configuration that was hand-edited or emitted by a code generator, so a pull request diff shows real changes instead of indentation noise.
  • Pasting a snippet from a tutorial, forum post, or older module into canonical layout before adding it to a codebase that already follows the style.
  • Normalizing a legacy or third-party module with mixed tabs, spaces, and ragged equals signs so it reads consistently before you take over maintaining it.

Limits and checks

  • Formatting is not validation. The tool checks syntax, not meaning: output that looks perfect can still reference undefined variables, use wrong argument names, or omit required blocks. Run terraform validate or terraform plan before applying anything.
  • Equals alignment depends on blank lines. The alignment run spans consecutive assignment lines and breaks at any blank line, so where you leave an empty line decides which lines align with each other.
  • It expects HCL text, not the JSON variant of Terraform configuration (.tf.json). JSON is not valid HCL syntax, so pasting a JSON file yields a parse error rather than formatted output.

Common questions

Does the output match what terraform fmt would produce?

For well-formed files, any faithful application of the canonical style from the HCL specification produces the same result, and that is what this tool aims at. If byte-for-byte equality with the CLI matters - say, a CI check that runs terraform fmt - run the real command, because it is the authoritative reference.

Will it sort my attributes alphabetically?

No. Formatting never reorders anything: argument order, block order, and object key order stay exactly as written. If a file is also out of order, fix the ordering yourself - a formatter cannot know the order you intend.

References and verification

The example and behavioral notes were checked against the browser implementation. Standards and primary references below define the relevant format, formula, or platform behavior.

Related Tools