Terraform Multiple Versions of Same Provider

'm having hard time working with modules of multiple versions, i even tried using Alias but its not working. Here’s an sample code. Can someone suggest where i’m doing wrong? or what’s the correct way.

providers = {
   azurerm = azurerm
   azurerm.permission = azurerm.permission
}


terraform {
  required_providers {
  azurerm = {
    source = "hashicorp/azurerm"
    version         = "~> 2.76.0"
  }
  azurerm = {
    source = "hashicorp/azurerm"
    version         = "~> 3.7.0"
    configuration_aliases = [azurerm.permission]
  }

provider "azurerm" {
  features {}
  subscription_id = data.vault_generic_secret.vault-spoke-spn.data["subscription_id"]
  tenant_id       = data.vault_generic_secret.vault-spoke-spn.data["tenant_id"]
  client_id       = data.vault_generic_secret.vault-spoke-spn.data["client_id"]
  client_secret   = data.vault_generic_secret.vault-spoke-spn.data["client_secret"]
}

provider "azurerm" {
  features {}
  alias = "permission"
  subscription_id = data.vault_generic_secret.vault-spoke-spn.data["subscription_id"]
  tenant_id       = data.vault_generic_secret.vault-spoke-spn.data["tenant_id"]
  client_id       = data.vault_generic_secret.vault-spoke-spn.data["client_id"]
  client_secret   = data.vault_generic_secret.vault-spoke-spn.data["client_secret"]
}

This syntax is not valid here - it’s only valid within a module block, for identifying which providers to pass down to the nested module. Just delete it from your top level Terraform code.


Hmm… I think this might get a bit messy.

Probably something like:

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 2.76.0"
    }
    azurerm3 = {
      source  = "hashicorp/azurerm"
      version = "~> 3.7.0"
    }
  }
}

and then you’d need to explicitly select the provider in all resources needing the one with the non-default name:

resource "azurerm_something" "something" {
  provider = azurerm3
}

Hi @maxb, Thanks for the reply.

  1. Actually the providers block was within the module.
  2. So i have made changes as per your suggestion, i have removed alias under provider:
    provider “azurerm3” {
    features {}
    #alias = “permission”
    subscription_id = data.vault_generic_secret.vault-spoke-spn.data[“subscription_id”]
    tenant_id = data.vault_generic_secret.vault-spoke-spn.data[“tenant_id”]
    client_id = data.vault_generic_secret.vault-spoke-spn.data[“client_id”]
    client_secret = data.vault_generic_secret.vault-spoke-spn.data[“client_secret”]
    }
    and also removed configuration_aliases but it didnt work. I tried with azurerm3 with aliases
    as well but same result.
  3. This is a module and there are few other modules which are inter-dependent and we dont want to change the versions, so thought there might be a way to use both versions on same provider(azurerm)

Below is the error which i get while i do terraform init:

Error: Failed to query available provider packages

│ Could not retrieve the list of available versions for provider hashicorp/azurerm: no available releases match the given constraints
│ ~> 2.76, ~> 3.7.0

Oh dear, Terraform has merged the two requirements blocks.

I fear then, it might be impossible to use two different versions of the provider, without taking a copy of one of them, and arranging for it to be available under a name other than "hashicorp/azurerm".

Yeah i googled almost all the pages but was not able to find the solution.

So i read somewhere about copying the provider and reusing it with another name.
by any chance, do you have any documentation where i can follow?

one last question, if i copy and use that renamed provider, will everyone in my Team should do that as its local on my computer right? like, how can they use that?storing in somewhere?