Speed up your Terraform init
In this short blog post I'll go over how you can speed up your terraform init step by caching provider data locally
Intro
By default, terraform will store provider information and their associated binaries in a .terraform
folder within work working directory. While this is okay if you're working with a single configuration, if you need to switch providers or state locations or anything else that requires re-downloading the providers this can become time-consuming (especially if you're working on a laggy client vpn)
Getting Started
The terraform cli allows for per-user configuration. This configuration is read from a file named .terraformrc
located in the root of the users home directory.
Firstly we need to create a new directory to store our provider cache:
mkdir -p ~/.terraform.d/plugin-cache
Next we need to configure the CLI to use this cache:
echo "plugin_cache_dir" = "$HOME/.terraform.d/plugin-cache" >> ~/.terraformrc
Now when you next run a terraform init
command, the providers will be cached to that directory and any subsequent init's will pull from there rather than the remote locations. Must faster!
Alternative Method
If you prefer, you can also make the same configuration to the cli via an environment variable, while of course this will only set the configuration per shell environment it can become handy if you ever need to temporarily override your cache directory for a specific use case. The syntax is as follows:
export TF_PLUGIN_CACHE_DIR="$HOME/.terraform.d/plugin-cache"
As with rc
file configuration method $home/.terraform.d/plugin-cache
must exist for this to work