terraform using different providers

 We can easily setup our terraform to support different providers. This is useful when we like to setup our resources using different subscription. 

modules/storage/main.tf


resource "azurerm_storage_account" "old_storage" {
  name                     = var.storage_name
  resource_group_name      = "mytest-kv-rg"
  location                 = "australiaeast"
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

variable "storage_name" {
  type = string
}


And then we have this as our main.tf 


# The default provider (Implicitly used if no provider is specified)
provider "azurerm" {
  features {}
  subscription_id = "aaaaaaaaaaaaaaaaaaaaa" # Subscription A
}

# The aliased provider
provider "azurerm" {
  alias           = "secondary"
  features {}
  subscription_id = "bbbbbbbbbbbbbbbbbbb" # Subscription B
}

module "primary-storage" {
  source       = "./modules/storage"
  providers    = { azurerm = azurerm }
  storage_name = "primarystorage2026jw"
}

module "secondary-storage" {
  source       = "./modules/storage"
  providers    = { azurerm = azurerm.secondary }
  storage_name = "secondarystorage2026jw"
}

That is all we need if we wanted to use different providers to setup our resources. Notice we did not declare additional provider block. This is cater for scenario where we assume all deployment goes to single subscriptions. 












Comments

Popular posts from this blog

vllm : Failed to infer device type

android studio kotlin source is null error

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20