How to Configure Terraform to Use Local Providers From Nexus

If your organization has blocked registry.terraform.io and has instead downloaded the provider binaries to Nexus, then you can do the following to still make your Terraform execute correctly.

Step 1 - Download the Required Providers

In our example, we need the following providers:

  1. AWS
  2. Archive

These commands below are running directly from the pipeline that executes the Terraform:

# Download the providers from the Nexus repository
- curl -u ${Nexus_REPO_USER}:${Nexus_REPO_PASS} -o terraform-provider-aws4.65.0linuxamd64.zip https://nexus.example.com/repository/some-local-mirror/registry.terraform.io/hashicorp/aws/terraform-provider-aws_4.65.0_linux_amd64.zip
- curl -u ${Nexus_REPO_USER}:${Nexus_REPO_PASS} -o terraform-provider-archive_2.3.0_linux_amd64.zip https://nexus.example.com/repository/local-mirror/registry.terraform.io/hashicorp/archive/terraform-provider-archive_2.3.0_linux_amd64.zip
# Make a local directory to store these providers
- mkdir -p $HOME/.terraform.d/plugins/registry.terraform.io/hashicorp/aws/
- mkdir -p $HOME/.terraform.d/plugins/registry.terraform.io/hashicorp/archive/
# Move the downloaded zip files to these directories
- mv terraform-provider-aws_4.65.0_linux_amd64.zip $HOME/.terraform.d/plugins/registry.terraform.io/hashicorp/aws/
- mv terraform-provider-archive_2.3.0_linux_amd64.zip $HOME/.terraform.d/plugins/registry.terraform.io/hashicorp/archive/
# Give the permissions (not always required)
- chmod 777 -R $HOME/.terraform.d/plugins/

Step 2 - Run the Terraform code with a Plugin Directory

The following code continues the pipeline from above where we left off:

# Add the "-plugin-dir" to use the same location as above
- terraform init -plugin-dir=$HOME/.terraform.d/plugins/ -backend-config=env/dev/backend.conf -reconfigure-force-copy

Step 3 - Update the terraform block to the same versions as above

Now we need to modify or add the following code into our Terraform code:

terraform {

  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "4.65.0"
    }
    archive = {
      source = "hashicorp/archive"
      version = "2.3.0"
    }
  }

  # Add other features you need here... e.g.
  # backend "s3" {
  #  ...
  #}

}