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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 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:

1
2
# 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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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" {
  #  ...
  #}

}