Configuring an Offline Terraform Plugin Cache for Offline Initialization
This guide explains how to set up a local Terraform plugin cache on Linux/Mac or Windows, configure the CLI to use it, and perform both online and offline initializations using the cached providers.
This article demonstrates how to configure an offline source for Terraform by creating a local plugin cache and adjusting the CLI configuration.
First, run terraform init once to download providers and store them in a cache directory. The guide uses Linux/Mac as the example system; Windows requires a different configuration file name.
For Linux/Mac, create a .terraformrc file (or terraform.rc on Windows) with the following content:
plugin_cache_dir = "${HOME}/.terraform.d/terraform-plugin-cache"
disable_checkpoint = trueThen create the cache directory:
mkdir -p $HOME/.terraform.d/terraform-plugin-cacheExport the environment variable so Terraform loads the configuration file:
export TF_CLI_CONFIG_FILE=$HOME/Desktop/terraform/terraform-module-example/.terraformrcInitialize the project with terraform init . Terraform will automatically download the required provider plugins and store them in the cache directory, as shown in the command output.
After initialization, you can verify the cached plugins under $HOME/.terraform.d/terraform-plugin-cache/registry.terraform.io/hashicorp/alicloud/1.164.0/… .
To simulate an offline environment, there are two methods:
Specify the plugin directory directly during initialization: terraform init --plugin-dir $HOME/.terraform.d/terraform-plugin-cache/
Define a local filesystem mirror in the Terraform configuration: provider_installation { filesystem_mirror { path = "/Users/lizeyang/.terraform.d/terraform-plugin-cache" include = ["registry.terraform.io/*/*"] } } Then run terraform init again, which will use the cached providers without contacting the remote registry.
Both methods complete the offline initialization successfully. The article notes that an alternative approach is to implement a custom Terraform registry server using the Terraform HTTP API and a Python Flask application.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.