Terraform Cleaner — A Tool to Detect Unused Variables and Locals in Your Modules

Sylvain Witmeyer
ITNEXT
Published in
2 min readMar 27, 2023

Have you ever worked on a legacy Terraform project with dozens of modules, especially when the project has a lot of technical debt? Refactoring can be quite challenging, right?

This is a common scenario that we often encounter when joining a team to improve the usability of Terraform. To start, I prefer to gain an understanding of how the code is constructed so that I can follow the rules and avoid breaking too many things. It would be nice to start by removing everything we don't care about

Link to Github Repo

On my last project, when we started analyzing the code to plan the refactor, I noticed that some variables and some locals were defined but not used in the module. There could be several valid reasons for this occurrence.

I strongly believe that we never spend enough time on cleaning up code. It’s such a critical topic that I’ll probably write a dedicated post about it. Unfortunately, this action is often perceived as a waste of time. People ask, “Why should we spend time on cleaning up when it works?” However, we tend to underestimate the amount of time engineers waste dealing with and questioning the existence of unnecessary pieces of code.

Anyway, instead of manually reviewing each variables I decided to write a small helper that could do the work for me. You can find terraform-cleaner at https://github.com/sylwit/terraform-cleaner

If you want to see the video, click on the link below or access it via Github

How to use it

Simply download the latest version for your platform (linux, mac, windows) from the release page https://github.com/sylwit/terraform-cleaner/releases/, make it executable and run it.

How it works

The terraform-cleaner tool recursively lists all Terraform modules, starting from the specified path passed as an argument. For each module, it extracts the list of all variables and locals and searches for their usage. The tool then collects statistics on their usage.

This tool only returns the count of variable and local usage, and it doesn’t delete anything. I strongly encourage you to manually review each variable or local to ensure that they are not necessary before deleting them.

What's next

Using Terraform Cleaner, I was able to detect more than 60 unused variables and locals, so I’m quite pleased with its performance. If you can think of other use cases or have suggestions for improvement, please feel free to open a pull request on GitHub or submit a ticket. I’m open to feedback and collaboration to make this tool even better.

Sign up to discover human stories that deepen your understanding of the world.

--

--

Published in ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect, collaborate, learn and experience next-gen technologies.

Written by Sylvain Witmeyer

AWS Solutions Architect at National Bank of Canada — https://sylwit.com

No responses yet

What are your thoughts?