How to waste less time and space with Terraform

I’ve been using Terraform quite extensively at work for some time and, over time, I’ve been interacting with more and more modules. At the same time, I need to work with a rather small HDD for modern standards where every GB counts.

Terraform’s default configuration wastes a lot of space by keeping a separate copy of the provider binary in each .terraform/plugins. In my case that was about 20 GB.

$ find . -name .terraform -exec echo {}/plugins \; | xargs du -hs --total

However, you can configure Terraform CLI to use a centalized cache directory as long as your OS supports hardlinks. To do so, create ~/.terraformrc with contents plugin_cache_dir = "$HOME/Library/Caches/terraform/plugin-cache" or some other directory that makes more sense to you. Read more about this configuration.

After that, you can take advantage of the consolidated cache.

$ mkdir -p ~/Library/Caches/terraform/plugin-cache
$ find . -name .terraform -exec rm -r {}/plugins \;
$ ...some terraform inits...
$ du -hs ~/Library/Caches/terraform/plugin-cache

On top of saved space you will have faster terraform inits.