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. 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. 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. 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.
| Aspect | Detail |
|---|---|
| Mode | Server-backed (no in-browser WASM) |
| Scope | Whitespace, indentation, spacing, basic layout |
| Reordering | No semantic reordering of resources or blocks |
| Options | None β single Format action for predictable output |
| Limits | Approx. 1β2 MB input, ~25s server timeout (subject to tuning) |
| Retention | Transient processing β input is discarded after formatting |
Language Coverage
Understands common HCL2 constructs used across Terraform and other HashiCorp tools.
| Construct | Examples | Notes |
|---|---|---|
| Blocks | resource, variable, output, module, locals, job, task | Nested block structure preserved |
| Attributes | name = "web", count = 2 | Spacing normalized around = and between attributes |
| Collections | [1, 2, 3], { key = value } | Uniform layout for lists and maps/objects |
| Heredocs | <<-EOF ... EOF | Markers preserved; indentation normalized where possible |
| Comments | # and // style comments | Kept 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"
}
}
Errors & Edge Cases
If formatting fails or returns an error, it is usually due to parsing problems in the HCL2 input.
| Symptom | Likely cause | What to check |
|---|---|---|
| No output / parse error | Unclosed brace, bracket or parenthesis | Count matching { }, [ ], ( ) pairs and close all blocks |
| Heredoc issues | Missing or mismatched terminator | Ensure markers like EOF appear exactly and on their own line |
| Mixed styles | Tabs/spaces or stray characters | Normalize indentation, remove stray control characters |
| .tfvars confusion | JSON vs HCL syntax | Use 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 fmtRewrites .tf and HCL-based .tfvars files in the current directory.
Terraform: recursive CI check (no writes)
terraform fmt -check -recursiveExits non-zero if any file needs formatting β perfect for CI and pre-commit hooks.
Packer: format template
packer fmt path/to/template.pkr.hclFormats 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 -recursiveUse 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 -recursiveStops 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.
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?
π‘οΈCan I paste secrets here?
π§©Does formatting change configuration behavior?
πCan I format .tfvars files?
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`?
π§―Why did formatting fail?
JSON-style tfvars with HCL syntax. Fix the structural issue (or use the correct dialect/formatter) and try again.Pro Tips
Enforce `terraform fmt -check -recursive` in CI to prevent formatting drift across large infrastructure repositories.
Avoid pasting secrets or state-related data into online tools; keep configuration examples sanitized and run local CLI formatters for real infrastructure code.
Normalize trailing newlines and indentation in your editor so Terraform files produce clean, stable diffs across platforms.
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
- CSS Beautifier
- HTML Beautifier
- Javascript Beautifier
- PHP Beautifier
- Color Picker
- Sprite Extractor
- Base64 Decoder
- Base64 Encoder
- Csharp Formatter
- Csv Formatter
- Dockerfile Formatter
- Elm Formatter
- ENV Formatter
- Go Formatter
- Graphql Formatter
- INI Formatter
- JSON Formatter
- Latex Formatter
- Markdown Formatter
- Objectivec Formatter
- Php Formatter
- Proto Formatter
- Python Formatter
- Ruby Formatter
- Rust Formatter
- Scala Formatter
- Shell Script Formatter
- SQL Formatter
- SVG Formatter
- Swift Formatter
- TOML Formatter
- Typescript Formatter
- XML Formatter
- YAML Formatter
- Yarn Formatter
- CSS Minifier
- Html Minifier
- Javascript Minifier
- JSON Minifier
- XML Minifier
- Http Headers Viewer
- PDF To Text
- Regex Tester
- Serp Rank Checker
- Whois Lookup