TFSort

July 22, 2023

terraformlogo

For meticulous engineers who value clean and organized code, managing Terraform variables and outputs can sometimes become a cumbersome task. Keeping track of these elements, especially as projects grow in complexity, may lead to scattered configurations and potential mistakes. Enter tfsort, a powerful command-line utility tailored to those who seek harmony in their Terraform code.

Installation:

For MacOS using HomeBrew (Install HomeBrew here):
  1. Run brew tap alexnabokikh/tfsort to add the tap
  2. Run brew install tfsort to install tfsort

brew

For Windows using Chocolatey (Install Chocolatey here):
  1. Run choco install tfsort

Also you can download latest binary release here

Let's see how it works now:

Sorting Variables

In order to make it simple i use varibales file with only 7 variables. Here is my file: sortfile

Let's try to sort it using TFSort and save sorted version to a new file - run tfsort <path-to-your-var-file> --out <path-to-your-sorted-file>

Let's check our sorted file: sorted As we can see now out variables are sorted alphabethically and easy to find

If you would like to simply overwrite your variable file run:

tfsort <pathtoyourvarfile>

Sorting Outputs

Here is a small outputs.tf file as example: outputs

Using same command we can sort it - tfsort <path-to-your-output-file> --out <path-to-your-sorted-output-file>

sortedoutputs