Read more

Terraform plugin cache

Florian Heinle
March 03, 2022Software engineer at makandra GmbH

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.

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show snapshot

To avoid creating multiple copies of the same file, you can use the Terraform Plugin Cache Show 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.

Posted by Florian Heinle to makandra Operations (2022-03-03 13:57)