Must have Terraform tools to make your coding journey smoother
A small set of tools to make your Terraform coding journey smoother. Tools to Lint, visualize, document, and clean your Terraform HCL2 code.
After a long gap of 11 months, I am back into my coding boots as I started building Terraform for a complex deployment project. Here are some handy tools that made my Terraform coding journey smoother :)
What is Terraform?
For starters, Terraform is an Infrastructure as a code tool that helps you provision Cloud or On-premise resources through declarative configuration. Terraform uses HCL2 (Hashicorp Configuration Language).
HCL2 = HCL1 + HIL (Hashicorp Interpolation Language)
Check this GitHub repository to learn more about HIL
Use different Terraform versions
If you are familiar with Node.js world, different projects require different versions.So, I use nvm(node version manager).
Similarly, in our Terraform world there are tfswitch
and tfenv
to use different versions of Terraform for different projects or for testing your Terraform project with different versions of Terraform.
Visualize Terraform
It helps to visualize your Terraform resources, modules etc., to understand the flow and the connections.
I published an article that talks about various visualization tools associated with Terraform. It includes the inbuilt terraform graph — https://medium.com/vmacwrites/tools-to-visualize-your-terraform-plan-d421c6255f9f
Code Formatting, code navigation, syntax highlighting
You can always run Terraform commands to format and validate your HCL2 code like
terraform fmt
terraform validate
Running these commands on a project with complex folder structure becomes tiresome as there can be multiple directories with *.tf
files.
- Visual Studio Code Extension to the rescue.
Add support for the Terraform configuration language to editors that use the Language Server Protocol, like Sublime Text, vim, emacs, etc. — Hashicorp
Documentation
Terraform-docs generates documentation for Terraform modules in multiple formats. The tool lists modules, resources, inputs, outputs, providers, data resources etc., The tool provides --recursive
flag to update submodules recursively.
Sample commands showing the markdown
format
terraform-docs markdown . >>README.md
terraform-docs markdown table .
Check a sample markdown file generated using Terraform-docs — https://github.com/VidyasagarMSC/vpc-private-k8s-public-lb/blob/master/terraform/README.md. The Terraform is associated with the article — https://medium.com/vmacwrites/multizone-kubernetes-and-vpc-load-balancer-setup-9664b3c9ea5d
Terraform Cleaner
As developers, we have a habit of forgetting things. Cleaning the code is one of the most important thing that we always forget. Terraform cleaner to the rescue.
Terraform Cleaner is a tool to detect Unused Variables and Locals in Your Modules. You can find the complete documentation in this medium article by the author.
Additional tools
Heard about terraform console
command? How many times as developers we would have thought of evaluating a list of objects or any other expression without running terraform plan or terraform apply
commands. terraform console
command helps you to evaluate and experiment with expressions on a terminal or command prompt. You can experiment with built-in terraform functions like slice, split, cat etc., even before incorporating them into your HCL2 code.
Additionally, you can check other tools for security & complaince, validation, automation etc., on the official Hashicorp documentation
++ Tools from the awesome-terraform GitHub repository