The Problem: Terraform Wasting Disk Space
If you’ve been using Terraform for a while, you’ve probably noticed that every time you initialize a new project (terraform init), it downloads all required provider plugins again—even if you already have them locally in another project. This redundant download not only wastes disk space but also increases initialization time, especially when working across multiple projects.
For teams working in CI/CD pipelines or locally with multiple repositories, this results in:
- Unnecessary duplication of plugins across projects.
- Increased disk space consumption over time.
- Longer
terraform inittimes, making workflows slower.
The good news? Terraform has a built-in solution: plugin caching.
Identifying the Issue: How Terraform Handles Plugins
By default, Terraform downloads provider plugins into the .terraform directory inside each project. This means every new Terraform project creates a fresh copy of the required providers, even if another project has already downloaded them.
You can check how much space Terraform is consuming by running:
du -sh ~/.terraform.d ~/.terraform
You can check in your whole disk, this command may take some time to complete:
find . -type f -name 'terraform-provider*' -exec du -ch {} +
Note: If you have a directory where you have all your terraform projects you may change the "." (that is the path to search) by your path (find /path/to/search ...).
If you have multiple projects, you'll likely see unnecessary duplication.
The way to solve this? Configure a shared plugin cache that Terraform can reuse across projects.
The Solution: Enabling Terraform Plugin Caching
Terraform allows you to store provider plugins in a shared directory instead of downloading them separately for each project. Here’s how to set it up:
1. Create a Plugin Cache Directory
First, create a centralized directory for Terraform to store plugins:
mkdir -p ~/.terraform.d/plugin-cache
This will serve as the global plugin repository for all your Terraform projects.
2. Configure Terraform to Use the Cache
Next, tell Terraform to use this directory by creating a .terraformrc configuration file:
echo 'plugin_cache_dir = "$HOME/.terraform.d/plugin-cache"' >> ~/.terraformrc
3. Verify the Configuration
Run terraform init inside any Terraform project. The first time, it will download plugins as usual, but from now on, Terraform will reuse the cached versions from ~/.terraform.d/plugin-cache, saving time and disk space.
To confirm it's working, check the cache directory:
ls ~/.terraform.d/plugin-cache
You should see provider plugins stored there instead of inside individual projects.
Final Thoughts
By configuring a Terraform plugin cache, you:
✅ Save disk space by eliminating redundant plugin downloads.
✅ Speed up Terraform init operations, especially across multiple projects.
Try out this approach and let me know how is being your experience with this. Happy Terraforming!!
Thank you for joining us on this adventure, and we look forward to seeing you on the next page.
- Story created without using AI tools -
Find out more in:
Linkedin: https://www.linkedin.com/in/rafael-domiciano/
Github: https://www.github.com/rafaeldomi
Twitter: https://twitter.com/rafaeldomiciano
Medium: https://medium.com/@rafael.domiciano