HCL Formatter - Re-indent Terraform Configuration Online
Re-indent Terraform and HCL files in the browser. Strings, heredocs and comments stay exactly as written, and the hashicorp/hcl parser reports syntax errors with their line and column.
How to Format HCL - Step by Step Guide
Input Your HCL/Terraform Configuration
Let's get your HCL (HashiCorp Configuration Language) into the formatter! Whether you're working with Terraform infrastructure code, need to format Terraform modules, or want to beautify your configuration files, you have several convenient options:
Example: Unformatted HCL Input
A typical file after a few rounds of copy and paste, with indentation all over the place:
resource "aws_instance" "web" {
ami = "ami-123456"
instance_type = "t2.micro"
tags = {
Name = "WebServer"
Environment = "prod"
}
}Automatic Formatting & Validation
About half a second after you stop typing, the page reads your HCL configuration and:
Example: Beautifully Formatted Output
The same configuration as it comes out of the formatter. Only the indentation and blank lines changed:
resource "aws_instance" "web" {
ami = "ami-123456"
instance_type = "t2.micro"
tags = {
Name = "WebServer"
Environment = "prod"
}
}Choose a Layout
There are two layouts to pick from:
Export Your Formatted HCL
Take the result back to your project or your DevOps workflow:
Frequently Asked Questions
How do I format messy Terraform/HCL configuration?
Paste it into the editor or upload the .tf file. About half a second after you stop typing, every line is re-indented with two spaces per level of braces, brackets or parentheses, trailing spaces are removed and runs of blank lines shrink to one. Nothing else on the line is touched.
Will formatting change my values, strings or heredocs?
No. Only the whitespace at the start and end of a line changes. Quoted strings, ${...} interpolations, numbers such as 8901240544102066246 or 14.50, and comments stay exactly as you typed them, and heredoc bodies (<<EOF and <<-EOT) are copied byte for byte, including their own indentation. Before showing the result, the page reads both versions with the HCL parser and checks they say the same thing; if they ever differed, you would get your file back unchanged with a note.
Does the formatter validate Terraform syntax?
It checks the syntax with hashicorp/hcl, the same parser Terraform uses, compiled to run in your browser. If the file cannot be read, you get the line and column of the first problem instead of a formatted copy. It does not know about providers, so an unknown resource type or a misspelled argument still passes; terraform validate catches those. The HCL Validator page shows more detail about errors.
Is the output the same as terraform fmt?
Not exactly. Both use two-space indentation, but terraform fmt also lines up the equals signs of neighbouring arguments and tidies spacing inside a line, which this page does not do. Run terraform fmt afterwards if your CI checks for its exact layout; it will only adjust spacing.
What does the Minify button do?
It removes indentation and blank lines so the file takes up fewer lines, which is handy for pasting a block into a chat or a ticket. HCL ends each argument at a line break, so lines are never joined together, and heredocs and comments are kept as they are. Editing the input switches back to the normal layout.
Is my configuration uploaded anywhere?
No. Parsing and formatting both happen in your browser, so nothing you paste or upload is sent to a server. That matters for Terraform files that hold account IDs or other details you would rather keep private. Very large files can take a moment, because the parser runs on your own machine.
For a closer look at errors, open the HCL Validator, or turn the file into JSON with HCL to JSON.
Related Tools
HCL to JSON
Convert HashiCorp Configuration Language (Terraform) to JSON format
JSON to HCL
Convert JSON to HashiCorp Configuration Language for Terraform
HCL Validator
Validate HCL/Terraform syntax and structure with error detection
HCL Fixer
Fix broken HCL/Terraform files automatically, repair syntax errors, missing braces, and malformed blocks
HCL to YAML
Convert Terraform HCL configuration to YAML format
YAML to HCL
Convert YAML configuration to Terraform HCL format