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):
- Run
brew tap alexnabokikh/tfsort
to add the tap - Run
brew install tfsort
to install tfsort
For Windows using Chocolatey (Install Chocolatey here):
- 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:
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: 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:
Using same command we can sort it - tfsort <path-to-your-output-file> --out <path-to-your-sorted-output-file>