Recently, I was working on a project that needed Terraform, but there was a catch; I had to work in an environment where there is no access to the Terraform Registry. So, I had to figure out how to use a Terraform provider locally, without relying on Terraform’s online registry. If you’re in a similar boat whether for testing, security, or any other reason then this guide is for you!
Terraform doesn’t just pick up providers from your directory automatically. You need to set things up manually, and that’s where my scripts come in.
Step 1: Get Your Provider Binary
First, grab your provider binary file (in this case, we’re using vsphere
with version 2.10.0
). Make sure it's in your working directory before running the script.
Step 2: Run the PowerShell Script (Windows)
Save the following script as setup-local-provider.ps1
and execute it in your PowerShell terminal:
# PowerShell script to setup local Terraform provider in current directory
$version = "2.10.0"
$provider = "vsphere"
$providerBinary = "terraform-provider-$provider`_v$version.exe"
# Function to create terraform.rc file
function Create-TerraformRC {
$rcContent = @"
provider_installation {
filesystem_mirror {
path = "$PWD\.terraform\plugins"
}
direct {
exclude = ["registry.terraform.io/*/*"]
}
}
"@
$rcPath = "$env:APPDATA\terraform.rc"
$rcContent | Out-File -FilePath $rcPath -Encoding UTF8 -Force
Write-Host "Created terraform.rc at: $rcPath"
}
# Function to create provider.tf file
function Create-ProviderConfig {
$tfContent = @"
terraform {
required_providers {
$provider = {
source = "hashicorp/$provider"
version = "$version"
}
}
}
"@
$tfPath = "provider.tf"
$tfContent | Out-File -FilePath $tfPath -Encoding UTF8 -Force
Write-Host "Created provider.tf configuration"
}
try {
# Verify provider binary exists in current directory
if (-not (Test-Path $providerBinary)) {
throw "Provider binary not found in current directory: $providerBinary"
}
# Create directory structure
$providerDir = ".terraform/plugins/registry.terraform.io/hashicorp/$provider/$version/windows_amd64"
New-Item -ItemType Directory -Force -Path $providerDir | Out-Null
Write-Host "Created provider directory structure"
# Copy provider binary
Copy-Item -Path $providerBinary -Destination "$providerDir/$providerBinary" -Force
Write-Host "Copied provider binary to: $providerDir/$providerBinary"
# Create terraform.rc file
Create-TerraformRC
# Create provider configuration
Create-ProviderConfig
Write-Host "`nSetup completed successfully!`n"
Write-Host "Next steps:"
Write-Host "1. Run terraform init -plugin-dir='.terraform/plugins' to initialize your working directory"
Write-Host "2. Create your terraform configuration files (.tf)"
Write-Host "3. Run 'terraform plan' to verify the setup"
} catch {
Write-Host "Error: $_" -ForegroundColor Red
exit 1
}
Step 3: Run the Linux Script (Linux/macOS)
Save the following script as setup-local-provider.sh
and execute it in your terminal:
#!/bin/bash
version="2.10.0"
provider="vsphere"
provider_binary="terraform-provider-$provider_v$version"
# Function to create terraform.rc file
create_terraform_rc() {
cat <<EOF > ~/.terraformrc
provider_installation {
filesystem_mirror {
path = "$PWD/.terraform/plugins"
}
direct {
exclude = ["registry.terraform.io/*/*"]
}
}
EOF
echo "Created terraform.rc at ~/.terraformrc"
}
# Function to create provider.tf file
create_provider_config() {
cat <<EOF > provider.tf
terraform {
required_providers {
$provider = {
source = "hashicorp/$provider"
version = "$version"
}
}
}
EOF
echo "Created provider.tf configuration"
}
# Main setup
if [ ! -f "$provider_binary" ]; then
echo "Error: Provider binary not found in current directory: $provider_binary" >&2
exit 1
fi
# Create directory structure
provider_dir=".terraform/plugins/registry.terraform.io/hashicorp/$provider/$version/linux_amd64"
mkdir -p "$provider_dir"
echo "Created provider directory structure"
# Copy provider binary
cp "$provider_binary" "$provider_dir/"
echo "Copied provider binary to: $provider_dir/$provider_binary"
# Create terraform.rc file
create_terraform_rc
# Create provider configuration
create_provider_config
echo -e "\nSetup completed successfully!\n"
echo "Next steps:"
echo "1. Run twtrito initialize your working directory"
echo "2. Create your terraform configuration files (.tf)"
echo "3. Run 'terraform plan' to verify the setup"
Step 4: Initialize Terraform
Run:
terraform init -plugin-dir='.terraform/plugins'
This tells Terraform to use the local plugin directory instead of trying to download the provider.
Step 5: Verify Your Setup
Try running:
terraform plan
If everything is set up correctly, Terraform should recognize the provider and not try to download it from the registry.
Top comments (0)