Terraform HCL Formatter

Format Terraform .tf HCL files with canonical indentation in your browser

Free in-browser Terraform HCL formatter. Paste your .tf configuration and get canonical 2-space indentation, aligned equals signs within argument runs, and collapsed blank lines — the same conventions terraform fmt enforces. Nothing is uploaded; your credentials stay safe. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

Is this the same as terraform fmt?

It applies the same canonical conventions terraform fmt enforces: 2-space indentation per nesting level, exactly one space around the equals sign in argument assignments, aligned equals within a run of single-line arguments, and collapsed blank lines. It is a structural reformatter rather than the full HashiCorp parser, so for byte-perfect output run terraform fmt in CI as well.

The Terraform HCL Formatter reformats HashiCorp Configuration Language to the canonical style that terraform fmt produces — without installing the Terraform CLI. Paste a .tf file and it returns a tidy version with consistent indentation and aligned arguments, entirely in your browser.

How it works

HCL has a small set of formatting conventions that the official formatter applies deterministically. This tool reproduces the most impactful ones:

  • 2-space indentation for each nesting level. Both { } blocks and [ ] collections increase depth.
  • Aligned = signs within a contiguous run of single-line argument assignments at the same depth. A blank line, comment, or nested block ends the run, so each group aligns independently.
  • One space around = in every assignment.
  • Collapsed blank lines — any run of two or more blank lines becomes a single blank line, and trailing whitespace is stripped.

The reformatter tracks nesting depth by counting brackets while ignoring those inside string literals, and it copies heredoc bodies (<<EOT ... EOT) verbatim so embedded scripts and JSON policies are never disturbed.

Example

Input:

resource "aws_instance" "web" {
ami="ami-123456"
instance_type =   "t3.micro"
}

Output:

resource "aws_instance" "web" {
  ami           = "ami-123456"
  instance_type = "t3.micro"
}

When to use this tool

  • Quick cleanup: You copied a snippet from documentation or a tutorial and want to normalise the indentation before dropping it into your codebase.
  • Code review: You are reviewing a pull request and want to confirm that the Terraform changes meet the canonical format without installing the CLI locally.
  • Sharing config: You are pasting HCL into a ticket, a chat, or a document and want it to look consistent.

For full module formatting in a local working directory, terraform fmt -recursive is the right tool. This formatter handles one file at a time in the browser.

Edge cases to know about

String content is never modified. Multi-line strings, heredoc blocks, and embedded JSON or scripts are copied verbatim. The formatter detects these contexts by tracking when it is inside a quoted string or between <<EOT / EOT markers, and does not apply indentation rules inside them.

Comments follow the surrounding block. Single-line (// or #) and block (/* */) comments are treated as code lines and indented along with the code at their depth.

Mixed-indentation input is handled. Whether your input uses tabs, 2 spaces, or 4 spaces, the output normalises to 2-space indentation. The input indentation is stripped by tracking bracket depth rather than copying existing whitespace.

Alignment runs break at blank lines. If you separate argument groups with a blank line, each group aligns its = signs independently. This matches terraform fmt behaviour and preserves the grouping intent of the original author.

Notes and tips

This is a structural reformatter, not the full HashiCorp parser. For most files the output matches terraform fmt, but for guaranteed byte-perfect formatting keep terraform fmt -check in your CI pipeline. Because everything runs locally, it is safe to paste configuration containing backend blocks or provider settings — nothing is transmitted off your machine.