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.0
For 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 init
Terraform 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.