Home > Software design >  Terraform Azure: How to use multiple version of the AzureRM provider?
Terraform Azure: How to use multiple version of the AzureRM provider?

Time:12-20

We have been using older versions of azurerm provider("~> 3.0.2") however to provision the "azurerm_private_dns_resolver" we need a new azurerm provider("~> 3.30.2").

The below terraform code throws the following error

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

# Azure Provider source and version being used
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.30.0"
    }  
  }
  required_version = ">= 1.1.0"
}

# Configure the Microsoft Azure provider
provider "azurerm" {
  version = "~> 3.0.2"

  features {}
}

provider "azurerm" {
  alias   = "latest"
  version = "~> 3.30.0"

  features {}
}


module "az-resource-group" {
  source = "./modules/resource_group/"

  providers = {
    azurerm = azurerm.latest
  }

  az_rg_name     = "example-resources"
  az_rg_location = "Central US"
}

Update: As mentioned, I tried to break the providers into module

enter image description here

enter image description here

enter image description here

Resource Group Module:

enter image description here

enter image description here

Still getting the same error.

CodePudding user response:

As of now, it is not possible to use the same provider with multiple versions in the same codebase. You can resolve this by having two options.

  • Separating the new resource which requires a new provider in another directory or any logical way which should have a dedicated/explicit provider config and state file. (not recommended as the code will be redundant)

Example directory structure.

.
├── .terraform
│   └── providers
│       └── registry.terraform.io
│           └── hashicorp
│               └── azurerm
│                   └── 3.0.2
│                       └── darwin_amd64
│                           └── terraform-provider-azurerm_v3.0.2_x5
├── .terraform.lock.hcl
├── latest_provider_directory
│   ├── .terraform
│   │   └── providers
│   │       └── registry.terraform.io
│   │           └── hashicorp
│   │               └── azurerm
│   │                   └── 3.32.0
│   │                       └── darwin_amd64
│   │                           └── terraform-provider-azurerm_v3.32.0_x5
│   ├── .terraform.lock.hcl
│   ├── providers.tf
│   └── new_resource.tf
├── providers.tf
└── old_resource.tf

Provider config or providers.tf

## provider config supports old versions.
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.0.2"

    }
  }
  required_version = ">= 1.1.0"
}
# Configure the Microsoft Azure provider
provider "azurerm" {
  features {}
}

## provider config supports new versions in latest_provider_directory to support resources with new provider versions.
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.32.0"
    }
  }
  required_version = ">= 1.1.0"
}

provider "azurerm" {
  features {}
}

  • Upgrading the current terraform code to the newest terraform-provider-azurerm provider. Usually, terraform-provider-azurerm does support backward compatibility but in some edge cases, it is possible that some arguments are deprecated or even modified. (recommended)
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.32.0" ## upgrading means updating this version from current to new(required) version.
    }
  }
  required_version = ">= 1.1.0"
}

provider "azurerm" {
  features {}
}

Note: Kindly init and run a plan on your existing code with the new provider version once you upgrade it and verifies that you get the below message either locally or done in pipelines.

No changes. Your infrastructure matches the configuration.

Also going through azurerm-terraform-provider-Changelog would be good to get more insights.

As a side note Version constraints inside provider configuration blocks are deprecated and will be removed in a future version of Terraform

Tested this warning with terraform version 1.3.5

CodePudding user response:

I believe you can use the alias feature to get around this https://developer.hashicorp.com/terraform/language/modules/develop/providers#provider-aliases-within-modules

will look something like below where you pass the right alias to the module

provider "azurerm" {
  alias   = "old"
  version = "~> 3.0.2"

  features {}
}

provider "azurerm" {
  alias   = "latest"
  version = "~> 3.30.0"

  features {}
}

module "az-resource-group" {
  source = "./modules/resource_group/"

  //Pass your preferred provider here
  providers = {
    azurerm = azurerm.latest
  }

  az_rg_name     = "example-resources"
  az_rg_location = "Central US"
}
  • Related