Learn how to leverage pre-installed Terraform plugins and streamline your infrastructure provisioning workflow by skipping the `terraform init` plugin download step.
Terraform provides flexibility in managing provider plugins. While plugins are typically downloaded during the terraform init phase, you can leverage pre-installed plugins for scenarios requiring specific plugin versions or environments with restricted internet access. This article outlines the steps to utilize pre-installed Terraform plugins.
Terraform typically downloads provider plugins during terraform init. However, you can use pre-installed plugins:
terraform plugins install <plugin_name> to install in the default plugin directory.TF_PLUGIN_CACHE_DIR environment variable. For example, for Linux on x86_64:
export TF_PLUGIN_CACHE_DIR="$HOME/.terraform.d/plugin-cache"
mkdir -p "$TF_PLUGIN_CACHE_DIR/linux_amd64"
mv <plugin_file> "$TF_PLUGIN_CACHE_DIR/linux_amd64"
terraform init: Terraform will now find and use the pre-installed plugin.Note: When using -plugin-dir with terraform init, ensure the directory structure within it matches the standard plugin layout (OS/architecture subdirectories).
This approach is useful in environments with limited internet access or when you need specific plugin versions.
This is an example of how to use a pre-installed AWS provider plugin with Terraform. First, download the correct plugin for your OS and architecture from the AWS provider releases page. Then, install the plugin by unzipping it and moving it to the correct directory. For Terraform 0.13+, use the terraform plugins install command. For older versions, unzip the plugin into the $HOME/.terraform.d/plugin-cache directory. Next, create a Terraform configuration file that specifies the AWS provider and version. Finally, initialize Terraform using the terraform init command. Terraform will now use the pre-installed AWS provider plugin. Remember to replace example values with your desired ones and adjust the instructions for other providers.
This example demonstrates using a pre-installed AWS provider plugin with Terraform.
1. Download the plugin:
.zip file for your OS and architecture (e.g., terraform-provider-aws_v4.20.0_linux_amd64.zip for Linux x86_64).2. Install the plugin:
For Terraform 0.13+:
unzip terraform-provider-aws_v4.20.0_linux_amd64.zip -d terraform-provider-aws_v4.20.0
terraform plugins install terraform-provider-aws_v4.20.0For older Terraform versions:
export TF_PLUGIN_CACHE_DIR="$HOME/.terraform.d/plugin-cache"
mkdir -p "$TF_PLUGIN_CACHE_DIR/linux_amd64"
unzip terraform-provider-aws_v4.20.0_linux_amd64.zip -d "$TF_PLUGIN_CACHE_DIR/linux_amd64"3. Create a Terraform configuration:
Create a file named main.tf with the following content:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "4.20.0" # Match the downloaded plugin version
}
}
}
# Example resource using the AWS provider
resource "aws_instance" "example" {
ami = "ami-0c55b159c9e670260" # Replace with your desired AMI
instance_type = "t2.micro"
}4. Initialize Terraform:
terraform initTerraform will now use the pre-installed AWS provider plugin.
Note:
-plugin-cache-dir flag for terraform init.TF_PLUGIN_CACHE_DIR: While the article mentions TF_PLUGIN_CACHE_DIR for older Terraform versions, you can actually use the -plugin-dir flag with terraform init for any Terraform version to specify a custom directory for plugins. This provides a consistent approach across versions.By understanding these nuances, you can effectively leverage pre-installed plugins in Terraform, enhancing control and flexibility in diverse deployment scenarios.
This document outlines how to use pre-installed Terraform provider plugins instead of downloading them during terraform init.
| Step | Description | Terraform Version |
|---|---|---|
| 1. Install the plugin | Download the appropriate plugin from the provider's releases page. | All |
| 2. Place the plugin |
Terraform 0.13+: Use terraform plugins install <plugin_name> for automatic installation. |
0.13+ |
Older versions: Place the plugin in a subdirectory (matching your OS and architecture) within the directory specified by the TF_PLUGIN_CACHE_DIR environment variable. |
Pre-0.13 | |
3. Run terraform init |
Terraform will automatically detect and use the pre-installed plugin. | All |
Key Points:
-plugin-dir: When using this flag with terraform init, ensure the directory structure follows the standard plugin layout (OS/architecture subdirectories).In conclusion, pre-installing Terraform plugins offers a valuable approach for managing infrastructure in environments with internet limitations or when specific plugin versions are essential. By understanding the steps involved in downloading, installing, and referencing these plugins, users can leverage Terraform's flexibility to streamline their workflow and ensure consistent deployments. Remember to prioritize security by verifying plugin sources and maintain compatibility between plugin and Terraform versions. As your automation needs grow, incorporating pre-installed plugins into CI/CD pipelines can further enhance efficiency and reproducibility. By mastering this technique, you gain greater control over your infrastructure provisioning process, enabling smoother and more reliable deployments.
Use Pre-Installed Terraform Plugins Instead of Downloading them ... | In normal usage, terraform init downloads & installs the plugins for any providers used in the configuration automatically in the .terraform directory. It’s sometimes desirable to disable…
Pre-installed Plugins not work as expected - Terraform - HashiCorp ... | Hi!! I’m working with different modules in my terraform repository and I want to pre-install in a folder and used it locally: Terraform Doc But I try to init my plugin folder with the following command and never find the modules: terraform init -plugin-dir ./terraform-plugins ... ... Initializing the backend... 2022-02-28T16:39:18.639+0100 [DEBUG] New state was assigned lineage "182fcd76-13c9-fce4-25f8-4e31fc6afd98" 2022-02-28T16:39:18.639+0100 [DEBUG] checking for provisioner in "./terrafor...
Command: init | Terraform | HashiCorp Developer | Re-running init with modules already installed will install ... terraform init will automatically find, download, and install the necessary provider plugins.
Terraform Init - Command Overview with Examples | What does the Terraform init command do? Learn how to init your infrastructure with Terraform. See examples and explore the available options.