terraform provider alias

Terraform provider alias allow us to have different configuration when provision resources. In Azure term, that would be creating different resources using different subscriptions.

Let's illustate this quickly.  Here we are configuring provider with different subscriptions 

# Default Provider (Subscription A)
provider "azurerm" {
  features {}
  subscription_id = "00000000-0000-0000-0000-000000000000"
}

# Secondary Provider (Subscription B)
provider "azurerm" {
  alias           = "sub_b"
  features {}
  subscription_id = "11111111-1111-1111-1111-111111111111"
}

And to use this provider here, we can try to create a resource group.

resource "azurerm_resource_group" "rg_in_sub_a" {
  name     = "primary-resources"
  location = "East US"
  # No provider specified, so it uses the default
}

resource "azurerm_resource_group" "rg_in_sub_b" {
  provider = azurerm.sub_b  # <--- Points to the alias
  name     = "secondary-resources"
  location = "West US"
}

And using it in a module, we can do the following :-


module "networking_sub_b" {
  source = "./modules/network"

  # Pass the alias into the module
  providers = {
    azurerm = azurerm.sub_b
  }

  vnet_name = "b-network"
}



Comments

Popular posts from this blog

vllm : Failed to infer device type

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

android studio kotlin source is null error