Providers Cache in OpenTofu and Terraform#
Having many small states has great benefits like fast tofu apply and separation of concerns, but it comes with a cost: OpenTofu downloads a provider to .terraform for every single directory.
With providers like azurerm or aws weighing in the 100s of megabytes, that accumulates fast.
I went from 800MB for a few states to 2MB by using a cache dir.
Here’s how:
mkdir -p ~/.cache/opentofu
cat <<'EOF' > ~/.tofurc
plugin_cache_dir = "$HOME/.cache/opentofu"
EOF
Docs#
Before#
tofu init in a fresh repo:
Initializing provider plugins...
- Reusing previous version of hashicorp/azurerm from the dependency lock file
- Installing hashicorp/azurerm v5.0.1...
- Installed hashicorp/azurerm v5.0.1 (signed, key ID 0C0AF313E5FD9F80)
tofu init again:
Initializing provider plugins...
- Reusing previous version of hashicorp/azurerm from the dependency lock file
- Using previously-installed hashicorp/azurerm v5.0.1
After#
tofu init in a fresh repo:
Initializing provider plugins...
- Reusing previous version of hashicorp/azurerm from the dependency lock file
- Installing hashicorp/azurerm v5.0.1 to the shared cache directory...
- Installed hashicorp/azurerm v5.0.1 (signed, key ID 0C0AF313E5FD9F80)
- Using hashicorp/azurerm v5.0.1 from the shared cache directory
tofu init again:
Initializing provider plugins...
- Reusing previous version of hashicorp/azurerm from the dependency lock file
- Using previously-installed hashicorp/azurerm v5.0.1
Werte