
tfsort is a command-line utility designed for meticulous engineers who prefer to keep their Terraform variable, output, locals and terraform blocks sorted alphabetically. It also corrects spacing issues between these blocks and removes leading or trailing newlines in the processed files. tfsort can read from files or stdin, write to files or stdout, and process directories recursively.
go installvariable, output, locals and terraform blocks within your HCL files..git) and Terraform utility directories (.terraform, .terragrunt-cache).tfsort processes files with the following extensions:
.tf.hcl.tofuTo install tfsort using Homebrew:
bash
brew tap alexnabokikh/tfsort
tfsort:bash
brew install tfsort
nix-env -iA nixpkgs.tfsort
To install tfsort using Chocolatey:
choco install tfsort
go installIf you have Go installed and configured, you can install tfsort directly using go install:
go install github.com/AlexNabokikh/tfsort@latest
This command will download the source code, compile it, and install the tfsort binary into your $GOPATH/bin or $GOBIN directory (make sure this directory is in your system's PATH).
Download the latest binary release for your operating system from the Releases Page.
Alternatively, build tfsort from source:
bash
git clone https://github.com/AlexNabokikh/tfsort.git
cd tfsort
bash
go build .
This will create a tfsort executable in the current directory.
tfsort [file_or_directory|-] [flags]
file_or_directory (optional):variables.tf).-r flag).- (optional):tfsort to read input from stdin.cat file.tf | tfsort), tfsort will read from stdin.tfsort will show the help message.-o, --out <path>:-o is not provided, the input file is overwritten.-o is not provided, the output is sent to stdout.-r, --recursive.-d, --dry-run:-r, --recursive:-d, --dry-run is also specified.-r is used, a directory path must be provided as an argument.-) or the -o, --out flag.-h, --help:-v, --version:tfsort application, typically including the version number, commit hash, and build date if available.variable, output, locals and terraform blocks in my_variables.tf and overwrites the file)bash
tfsort my_variables.tf
bash
tfsort my_variables.tf -o sorted_variables.tf
my_variables.tf)bash
tfsort my_variables.tf -d
bash
cat my_config.tf | tfsort -
Or, if tfsort is part of a pipeline and no file argument is given:
bash
cat my_config.tf | tfsort
bash
cat my_config.tf | tfsort - -o sorted_from_stdin.tf
.tf, .hcl, .tofu files in my_terraform_project/ and its subdirectories, modifying them in-place. Skips .git, .terraform, .terragrunt-cache.)bash
tfsort -r ./my_terraform_project/
bash
tfsort -r ./my_terraform_project/ -d
Contributions are welcome! Please read the CONTRIBUTING.md file for guidelines on how to contribute to this project, including code contributions, bug reports, and feature suggestions.
This project adheres to the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code. Please read the CODE_OF_CONDUCT.md file for details.
This project was created by Alexander Nabokikh.
This software is available under the following licenses:
$ claude mcp add tfsort \
-- python -m otcore.mcp_server <graph>