Use Multiple version of azurerm in terrafom code

Viewed 31

I Have a terraform code which use module inside a module.

main.tf

module "FunctionApp" {
  source              = "../modules/FunctionApp"
  location            = var.location
  resourceGroupName   = module.rg.name
}

module FunctionApp - main.tf

resource "azurerm_resource_group" "example" {
  name     = "azure-functions-test-rg"
  location = "westus2"
}
resource "azurerm_storage_account" "example" {
  name                     = "functionsappsacostco"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
  min_tls_version = "TLS1_2"
}


resource "azurerm_app_service_plan" "example" {
  name                = "azure-functions-test-service-plan"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  kind = "windows"
  sku {
    tier = "PremiumV2"
    size = "P1v2"
    }
}

resource "azurerm_function_app" "example" {
  name                       = "test-azure-functions-csco"
  location                   = azurerm_resource_group.example.location
  resource_group_name        = azurerm_resource_group.example.name
  app_service_plan_id        = azurerm_app_service_plan.example.id
  storage_account_name       = azurerm_storage_account.example.name
  storage_account_access_key = azurerm_storage_account.example.primary_access_key
  version = "~4"


}
module "fz_slot" {
source = "./fz-slot"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
app_service_plan_id = azurerm_app_service_plan.example.id
function_app_name = azurerm_function_app.example.name
storage_account_name = azurerm_storage_account.example.name
storage_account_access_key = azurerm_storage_account.example.primary_access_key
}

module fz-slot - main.tf

resource "azurerm_function_app_slot" "example" {
  provider                   = azurerm.old
  name                       = "staging"
  location                   = var.location
  resource_group_name        = var.resource_group_name
  app_service_plan_id        = var.app_service_plan_id
  function_app_name          = var.function_app_name
  storage_account_name       = var.storage_account_name
  storage_account_access_key = var.storage_account_access_key
  version = "~4"
}

I want to use the latest azurerm version in all the resources other than fz-slot module. In fz-slot module i want to use azurerm 2.67.0 version.

How can I achive this ?

1 Answers

You must declare two providers

The default provider:

provider "azurerm" {
  version = "~>3.23.0"
  features {}
}

The provider for fz-slot module:

provider "azurerm" {
  alias = older
  version = "2.67.0"
  features {}
}

In your module, you must indicate the provider:

module "fz_slot" {
  source = "./fz-slot"
  providers = {
   azurerm = azurerm.older
  }
  location = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  app_service_plan_id = azurerm_app_service_plan.example.id
  function_app_name = azurerm_function_app.example.name
  storage_account_name = azurerm_storage_account.example.name
  storage_account_access_key = 
  azurerm_storage_account.example.primary_access_key
}

Hope this helps!

Related