A powerful command-line tool for sorting and formatting Terraform (.tf) and Terragrunt (.hcl) files to ensure consistency and readability across your infrastructure code.
- Smart Block Sorting: Orders Terraform blocks for readability and best practices.
- Attribute Sorting: Alphabetizes attributes, with special handling for
count,for_each, andname. - Nested Block Support: Handles deeply nested and complex HCL structures.
- IAM Policy Document Exclusion: Option to exclude
aws_iam_policy_documentdata blocks from sorting. - Formatting: Applies
terraform fmtstandards. - Multiple Modes: Dry-run, validation, and recursive directory processing.
- Comprehensive Error Handling: Detailed, colorized error messages.
- Cross-Platform: Works on macOS, Linux, and Windows.
- Tested & Modular: Well-tested, DRY, and maintainable codebase.
# Install (from source)
git clone https://github.com/yourusername/sortTF.git
cd sortTF
go build -o sorttf
sudo mv sorttf /usr/local/bin/
# Or using Go install
go install github.com/OBerger96/sortTF@latest
# Basic usage
sorttf .- Go 1.24.4+ (for building from source)
- Terraform (for formatting functionality)
sorttf . # Sort and format files in current directory
sorttf main.tf # Sort and format a specific file
sorttf --recursive . # Recursively process subdirectories
sorttf --dry-run . # Show what would change without writing
sorttf --validate . # Validate files without making changes
sorttf --verbose . # Verbose output
sorttf --exclude-iam-policy-documents . # Exclude aws_iam_policy_document data blocks| Flag | Description |
|---|---|
--recursive |
Scan directories recursively |
--dry-run |
Show what would be changed without writing (shows a unified diff) |
--verbose |
Print detailed logs about which files were parsed, sorted, and formatted |
--validate |
Exit with a non-zero code if any files are not sorted/formatted |
--exclude-iam-policy-documents |
Exclude aws_iam_policy_document data blocks from sorting |
sorttf main.tf
sorttf --recursive --verbose .
sorttf --validate --recursive .
sorttf --dry-run --recursive .
sorttf --exclude-iam-policy-documents --recursive .sortTF sorts Terraform blocks in the following order:
- terraform
- provider
- variable
- locals
- data
- resource
- module
- output
countorfor_eachare always placed first (if present, they are mutually exclusive)nameattribute is placed second (if present)- Other attributes are sorted alphabetically
- Nested blocks are sorted by type and then by labels
Before:
resource "aws_instance" "web" {
instance_type = "t3.micro"
ami = "ami-123456"
tags = { Name = "web-server" }
}
provider "aws" { region = "us-west-2" }
variable "environment" { type = string }After:
provider "aws" { region = "us-west-2" }
variable "environment" { type = string }
resource "aws_instance" "web" {
ami = "ami-123456"
instance_type = "t3.micro"
tags = { Name = "web-server" }
}go test ./... # Run all tests
go test -v ./... # Verbose output
go test -cover ./... # Run with coveragesortTF/
βββ main.go # Application entry point
βββ utils/
β βββ cliutil/ # Command-line interface
β βββ argsutil/ # CLI argument parsing
β βββ errorutil/ # Error helpers and types
β βββ fileutil/ # File system operations
β βββ formattingutil/ # HCL formatting
β βββ parsingutil/ # HCL parsing and validation
β βββ sortingutil/ # Block and attribute sorting
# Build for current platform
go build -o sorttf
# Build for specific platforms
GOOS=linux GOARCH=amd64 go build -o sorttf-linux
GOOS=darwin GOARCH=amd64 go build -o sorttf-darwin
GOOS=windows GOARCH=amd64 go build -o sorttf-windows.exe# Run linter
go vet ./...
# Format code
go fmt ./...
# Run tests with coverage
go test -cover ./...- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow Go coding standards
- Add tests for new features
- Update documentation as needed
- Ensure all tests pass before submitting PR
This project is licensed under the MIT License - see the LICENSE file for details.
sortTF does not preserve inline comments (comments on the same line as code). This is a fundamental limitation of the HCL writer library used by the tool.
Example:
# Before sorting
resource "aws_instance" "example" {
limit_amount = "100" # Not used due to https://github.com/hashicorp/terraform-provider-aws/issues/28981
instance_type = "t3.micro"
}
# After sorting - inline comment is lost
resource "aws_instance" "example" {
instance_type = "t3.micro"
limit_amount = "100"
}Workaround: Place important comments on their own line above the attribute:
resource "aws_instance" "example" {
instance_type = "t3.micro"
# Not used due to https://github.com/hashicorp/terraform-provider-aws/issues/28981
limit_amount = "100"
}Block-level comments (comments on their own lines) are preserved and will move with the blocks they precede.
- Requires Terraform to be installed for formatting functionality
- Some edge cases with deeply nested blocks may need manual review
- Issues: GitHub Issues
- Built with HashiCorp HCL
- Inspired by
terraform fmtand similar formatting tools - Thanks to the Go community for excellent tooling and libraries