Loading…

About Online HCL / Terraform Formatter

Terraform, Packer and other HashiCorp tools all speak HCL2β€”and they all look better when consistently formatted. This HCL Formatter sends your configuration to a secure backend that applies terraform fmt–style rules to indentation, spacing and layout, then returns normalized HCL2 while leaving semantics untouched.

Key Features

  • HCL2-aware formatting for Terraform-style blocks, attributes, lists/maps and heredocs
  • Consistent indentation and spacing around =, commas, braces and nested blocks
  • Preserves comments and intentional blank lines where possible for readability
  • Works with .tf, .hcl, HCL-based .tfvars and Packer .pkr.hcl files
  • Idempotent output: running the formatter again yields the same result
  • Minimal surface area: a single β€œFormat” action, no style knobs to argue about
  • One-click copy or download of the formatted result from the editor
  • Server-backed processing via a secured endpoint β€” great for quick fixes; prefer local CLI for highly sensitive code

πŸ”§ How to Use the HCL Formatter for hcl-formatter

1

1. Paste or Drop Your Code

Open your HCL/Terraform file (.tf, HCL-based .tfvars, .hcl, .pkr.hcl, etc.) and paste the contents into the editor, or drop the file if your browser supports it.

2

2. Click β€œFormat”

Click the Format button. Your configuration is sent to a secure backend that applies terraform fmt–style rules to indentation, spacing and layout, then returns formatted HCL2.

3

3. Review, Copy or Download

Check the result in the editor, then copy it back into your IDE or download the formatted file and commit it to your repository.

Technical Specifications

Execution Model

The formatter is server-backed and designed to mimic terraform fmt–style layout normalization for HCL2.

AspectDetail
ModeServer-backed (no in-browser WASM)
ScopeWhitespace, indentation, spacing, basic layout
ReorderingNo semantic reordering of resources or blocks
OptionsNone β€” single Format action for predictable output
LimitsApprox. 1–2 MB input, ~25s server timeout (subject to tuning)
RetentionTransient processing β€” input is discarded after formatting
As with any online tool, avoid pasting production secrets. Use terraform fmt or packer fmt locally for sensitive code.

Language Coverage

Understands common HCL2 constructs used across Terraform and other HashiCorp tools.

ConstructExamplesNotes
Blocksresource, variable, output, module, locals, job, taskNested block structure preserved
Attributesname = "web", count = 2Spacing normalized around = and between attributes
Collections[1, 2, 3], { key = value }Uniform layout for lists and maps/objects
Heredocs<<-EOF ... EOFMarkers preserved; indentation normalized where possible
Comments# and // style commentsKept where possible to preserve intent and documentation

Mini Before/After

A small example of indentation and spacing cleanup in a Terraform resource.

# Before
resource "aws_s3_bucket" "b"{bucket="demo"
  tags={Name="demo"}}

# After
resource "aws_s3_bucket" "b" {
  bucket = "demo"

  tags = {
    Name = "demo"
  }
}
Layout-only transformation: identifiers, expressions and references remain unchanged.

Errors & Edge Cases

If formatting fails or returns an error, it is usually due to parsing problems in the HCL2 input.

SymptomLikely causeWhat to check
No output / parse errorUnclosed brace, bracket or parenthesisCount matching { }, [ ], ( ) pairs and close all blocks
Heredoc issuesMissing or mismatched terminatorEnsure markers like EOF appear exactly and on their own line
Mixed stylesTabs/spaces or stray charactersNormalize indentation, remove stray control characters
.tfvars confusionJSON vs HCL syntaxUse JSON formatter or terraform fmt for *.tfvars.json files

Command Line Alternatives

For day-to-day Infrastructure as Code workflows, use official formatters locally and in CI, and keep this online tool for quick ad-hoc cleanups.

macOS / Linux

Terraform: format in place

terraform fmt

Rewrites .tf and HCL-based .tfvars files in the current directory.

Terraform: recursive CI check (no writes)

terraform fmt -check -recursive

Exits non-zero if any file needs formatting β€” perfect for CI and pre-commit hooks.

Packer: format template

packer fmt path/to/template.pkr.hcl

Formats the specified Packer HCL file in place.

Windows (PowerShell)

Format all Terraform files recursively

Get-ChildItem -Recurse -Filter *.tf | ForEach-Object { terraform fmt $_.FullName }

Runs terraform fmt on each .tf file found under the current directory.

CI-style formatting check

terraform fmt -check -recursive

Use in build pipelines to fail when formatting is out of date.

GitHub Actions

Fail the build if formatting is required

steps:
  - uses: hashicorp/setup-terraform@v3
  - run: terraform fmt -check -recursive

Stops the workflow when any Terraform file is not properly formatted.

Auto-format on pull requests (optional)

steps:
  - run: terraform fmt -recursive
  - run: git diff --quiet || (git config user.name "bot" && git config user.email "bot@example" && git commit -am "chore: terraform fmt" && git push)

Applies terraform fmt and pushes a formatting-only commit back to the branch.

For regulated or confidential infrastructure code, prefer local CLI tools and enforce `terraform fmt -check -recursive` in CI instead of sending files to online formatters.

Practical Applications

PR Hygiene & Reviews

Normalize whitespace and indentation so reviewers can focus on actual infrastructure changes.

  • Run formatting before opening a pull request
  • Reduce nitpicky style comments in Terraform reviews
  • Keep git diffs small and meaningful

Terraform Module Development

Keep modules consistent across teams, repos and registries.

  • Align variable and output blocks in shared modules
  • Ensure example configurations match a single house style

CI Gatekeeping

Use terraform fmt checks in CI to prevent style drift over time.

  • Add `terraform fmt -check -recursive` as a mandatory pipeline step
  • Block merges until all .tf files are formatted

Onboarding & Docs

Readable examples help new team members learn both Terraform and house style quickly.

  • Publish consistently formatted snippets in READMEs
  • Use clean examples in internal Terraform training sessions

❓ Frequently Asked Questions

πŸ”’Is my code processed locally?

No. This HCL formatter is server-backed: your input is sent to a secure endpoint for formatting and the resulting HCL is returned to your browser. For highly sensitive or regulated infrastructure code, prefer running `terraform fmt` (or `packer fmt`) locally.

πŸ›‘οΈCan I paste secrets here?

We strongly recommend you do not paste production secrets into any online tool. Even though the service is secured and designed for transient processing, long-lived or highly sensitive credentials should only live in local files, secret stores or CI vaults β€” not in browser tools.

🧩Does formatting change configuration behavior?

No. The formatter focuses on layout: whitespace, indentation and spacing. Resource blocks, arguments, expressions and references remain the same, assuming the original HCL was valid.

πŸ“„Can I format .tfvars files?

Yes, as long as the .tfvars file uses HCL syntax. Terraform also supports JSON-based variables via `.tfvars.json`; those follow JSON rules and are better handled by a JSON formatter or by `terraform fmt` itself.

βš™οΈHow is this different from `terraform fmt`?

`terraform fmt` is the canonical formatter shipped with Terraform and is ideal for local development and CI. This web tool is meant for quick, ad-hoc cleanups when you don’t have the CLI handy or want to tidy up a small snippet in the browser.

🧯Why did formatting fail?

Most failures are parse errors: unclosed braces or brackets, malformed heredocs, or mixing JSON-style tfvars with HCL syntax. Fix the structural issue (or use the correct dialect/formatter) and try again.

Pro Tips

CI Tip

Enforce `terraform fmt -check -recursive` in CI to prevent formatting drift across large infrastructure repositories.

Security Tip

Avoid pasting secrets or state-related data into online tools; keep configuration examples sanitized and run local CLI formatters for real infrastructure code.

Best Practice

Normalize trailing newlines and indentation in your editor so Terraform files produce clean, stable diffs across platforms.

Best Practice

Add a pre-commit hook that runs `terraform fmt` so pull requests arrive already formatted and review conversations can stay focused on architecture and risk.

Additional Resources

Other Tools