Michu
Michu

Reputation: 149

multiple terraform provider versions in local provider directory

I wanted to abandon terraform-bundle binary in exchange for recommended solutions described in here https://github.com/hashicorp/terraform/tree/main/tools/terraform-bundle (we are upgrading terraform to version 1.x atm)

but I’m facing problem with specifying multiple versions of the same provider. In past our code looked like this (when using terraform-bundle):

tf-bundle.hcl:

terraform {
  version = "0.14.11"
}

providers {
  aws = {
    source   = "hashicorp/aws"
    versions = [""3.75.0", "3.75.2"]
  }

and we were able to simply run terraform-bundle and we have two versions of aws provider saved locally. However, it seems like it is not possible with terraform providers mirror command.. How can I store it locally and not to use terrafom-bundle binary?

Upvotes: 0

Views: 1168

Answers (1)

Martin Atkins
Martin Atkins

Reputation: 74299

A single Terraform configuration can only depend on one version of each provider at a time. There is no syntax for specifying multiple versions.

I'm assuming that you're trying to use terraform providers mirror to populate a mirror directory.

That command is designed to work with one configuration at a time and mirror the exact dependencies currently required for that configuration. You can run it multiple times in different working directories with the same target directory to mirror potentially many versions of the same provider, but each single configuration can only possibly contribute one version of each provider.

cd config1
terraform providers mirror ~/.terraform.d/plugins
cd ../config2
terraform providers mirror ~/.terraform.d/plugins

The second call to terraform providers mirror will update ~/.terraform.d/plugins to include additional provider packages if needed, while preserving the ones that were placed there by the first call.


If your goal is only to create a local directory to use as a "filesystem mirror" then you don't need the metadata JSON files that terraform providers mirror generates, since those are only for network mirrors.

In that case, the only requirement is to match one of the two supported directory structures, and so you can potentially construct such a directory without Terraform's help:

mkdir ~/.terraform.d/plugins
cd ~/.terraform.d/plugins
mkdir -p registry.terraform.io/hashicorp/aws/3.75.0/linux_amd64
wget https://releases.hashicorp.com/terraform-provider-aws/3.75.0/terraform-provider-aws_3.75.0_linux_amd64.zip
unzip terraform-provider-aws_3.75.0_linux_amd64.zip -d registry.terraform.io/hashicorp/aws/3.75.0/linux_amd64
wget https://releases.hashicorp.com/terraform-provider-aws/3.75.2/terraform-provider-aws_3.75.2_linux_amd64.zip
unzip terraform-provider-aws_3.75.2_linux_amd64.zip -d registry.terraform.io/hashicorp/aws/3.75.2/linux_amd64

After running the above commands, the ~/.terraform.d/plugins directory will contain two specific versions of the AWS provider. That is one of the implied local mirror directories, so unless you've overridden this behavior with a custom provider_installation block in your CLI configuration Terraform should then install that provider only from this local directory and not from the origin registry.

(I've assumed that you are using Linux above; if not then the details will be slightly different but the effect still the same. Specifically, you'll need to use the correct platform name instead of linux_amd64 in the directory paths and URLs; you can see the platform name for your current platform by running terraform version.)

Upvotes: 1

Related Questions