Terraform plugin cache

Posted . Visible to the public.

If you're using many terraform root modules in a repository, downloading common providers like the AWS one can quickly add up to a lot of storage space wasted. Each root module will download a copy of the same provider file. The AWS provider is over 200 MB in size.

To avoid creating multiple copies of the same file, you can use the Terraform Plugin Cache Show archive.org snapshot .

Configuring the plugin cache:

It's possible to add the following setting to $HOME/.terraformrc:

plugin_cache_dir   = "$HOME/.terraform.d/plugin-cache"

Another possibility is to set an environment variable when you run terraform init:

TF_PLUGIN_CACHE_DIR="$HOME/.terraform.d/plugin-cache" terraform init

Setting the variable when running init is good enough, apply does not need it.

How it works

When you run terraform init in a root module directory, it will download all required providers to the .terraform/providers directory. If the plugin cache setting is active, it will instead download it to the specified cache directory and create a symlink to the .terraform/providers directory.

terraform apply will just follow the symlink that has been created by init and will not need to know about the cache setting.

Migrating existing setups

There is no automatic migration to the caching mechanism. If you already have a working directory where providers have already been downloaded and wish to enable plugin caching to save disk space, you can just delete the sub directories in .terraform/providers and re-run TF_PLUGIN_CACHE_DIR="$HOME/.terraform.d/plugin-cache" terraform init.

Emma Heinle
Last edit
Emma Heinle
License
Source code in this card is licensed under the MIT License.