Discover a tiny utility designed to analyze your Terraform modules and identify unused variables and locals. This lightweight yet effective tool streamlines your infrastructure-as-code development by highlighting redundant code, leading to cleaner and more efficient Terraform projects. Whether you're an experienced Terraform user or a beginner, this invaluable module analysis tool enhances your coding experience, ensuring optimized and maintainable infrastructure configurations. Elevate your Terraform module development with this essential utility.
Let's install Terraform Cleaner and and see how it works in our example:
To install Terraform Cleaner on your PC:
- Visit Git Hub repo and download binary for your platform
- Make sure it is executable
In our example i will use simple terraform code:
# Variables
variable "region" {
default = "us-east-1"
}
variable "unused1" {
default = "1"
}
variable "unused2" {
default = "1"
}
#Provider
provider "aws" {
region = var.region
}
resource "aws_instance" "my_instance" {
instance_type = "t3.micro"
ami = "ami-052efd3df9dad4825"
#key_name = "misha-2021-19-nov"
user_data = file("userdata.tpl")
}
# Default VPC
resource "aws_default_vpc" "default" {
}
Let's check this code with TFcleaner:
- Run
<PATH TO YOUR TFCLEANER BINARY> <PATH TO YOUR TF PROJECT FOLDER>
Output shows every variable or locals written in our code and how many times was used.
Please Note: If variable was never used in your code you will see
0
in usage field, that typically means that it is not really needed
- Also we can specify to show only unused we can run
<PATH TO YOUR TFCLEANER BINARY> <PATH TO YOUR TF PROJECT FOLDER> --unused-only
For more info regarding commands run <PATH TO YOUR TFCLEANER BINARY> --help
The tool offers an intuitive and user-friendly experience, making it accessible to both seasoned Terraform experts and newcomers. Its straightforward interface allows users to effortlessly analyze their Terraform modules, enabling a deeper understanding of their codebases. By swiftly detecting and highlighting unused variables and locals, developers can easily identify areas of improvement and eliminate unnecessary complexities in their infrastructure-as-code projects.
Thanks to its lightweight design and powerful capabilities, the tool seamlessly integrates into existing development workflows, enhancing productivity and collaboration.
Feel free to check their oficial Git Hub page