Search code examples
terraformterraform-provider-awsterraform-provider-azureterraform-provider-gcphashicorp

Reusability of terraform providers package


Suppose, there is 2 terraform projects (A & B) and both may have common providers. Below is the providers directory architecture. One plus pointed is, terraform already manage providers download binaries in distinct directory named on their version (eg: http provider have 3.0.0 and 3.1.0).

I am looking for terraform configuration something similar to maven local repository where it keeps all the downloaded binaries at common place ~/.m2 (default) and major advantages it can avoid duplicacy (or unnecessary disk space usage).

${path.module} ==> tree .terraform/providers/
.terraform/providers/
└── registry.terraform.io
    └── hashicorp
        ├── http
        │   ├── 3.0.0
        │   │   └── darwin_arm64
        │   │       └── terraform-provider-http_v3.0.0_x5
        │   └── 3.1.0
        │       └── darwin_arm64
        │           └── terraform-provider-http_v3.1.0_x5
        └── null
            └── 3.1.1
                ├── darwin_amd64
                │   └── terraform-provider-null_v3.1.1_x5
                └── darwin_arm64
                    └── terraform-provider-null_v3.1.1_x5

11 directories, 4 files

I am not sure whether this RE-USABLE approach would be achievable or not. In case of not, would like to understand strategy behind it.


Note: For remote module, want to keep same directory path as it is ${path.module}/.terraform/modules/

${path.module} ==> tree .terraform/modules/
.terraform/modules/
├── modules.json
└── vpc
    ├── CHANGELOG.md
    ├── LICENSE
    ├── README.md
    ├── UPGRADE-3.0.md
    ├── ......


Solution

  • Yes you can do this with the -plugin-dir argument to terraform init. This will configure and utilize a shared directory on the local filesystem for the plugins for each of the root module directories initialized in this manner as long as you specify the same directory for each. However, this may have undesired side effects, so in practice it is not necessarily recommended.