Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terrafrom - Deploy to multiple azure subscriptions

I have been trying to use the same terraform stack to deploy resources in multiple azure subscriptions. Also need to pass parameters between these resources in different subscriptions. I had tried to use multiple Providers, but that is not supported.

Error: provider.azurerm: multiple configurations present; only one configuration is allowed per provider

If you have a way or an idea on how to accomplish this please let me know.

like image 866
Parvez Avatar asked Aug 06 '18 19:08

Parvez


People also ask

Can I have multiple Azure subscriptions?

Organizations can have multiple subscriptions for Microsoft's cloud offerings. Figure 1 shows a single organization that has multiple Microsoft 365 subscriptions, a Dynamics 365 subscription, and multiple Azure subscriptions.

Can Azure resource Group have multiple Azure subscriptions?

Each Azure subscription can contain multiple account administrators. Each Azure subscription can be managed by using a Microsoft account only. An Azure resource group contains multiple Azure subscriptions.


2 Answers

You can use multiple providers by using alias (doku).

# The default provider configuration
provider "azurerm" {
  subscription_id = "xxxxxxxxxx"
}

# Additional provider configuration for west coast region
provider "azurerm" {
  alias  = "y"
  subscription_id = "yyyyyyyyyyy"
}

And then specify whenever you want to use the alternative provider:

resource "azurerm_resource_group" "network_x" {
  name     = "production"
  location = "West US"
}

resource "azurerm_resource_group" "network_y" {
  provider = "azurerm.y"
  name     = "production"
  location = "West US"
}
like image 199
Markus Avatar answered Oct 27 '22 03:10

Markus


Markus answer is correct, but it is the right solution if you need to access more than one subscription in the same set of Terraform sources.

If your purpose is to use one subscription as sandbox and the other for real, you should simply move the provider information out of Terraform scripts. There are more than one way to manage this:

  • Workspaces
  • Backend configuration
  • A wrapper script in bash/Powershell/python in Terragrunt style
  • Symbolic links can also be used to share files in multiple folders

I use a combination of the last three as workspaces are too rigid for our needs.

like image 25
Giulio Vian Avatar answered Oct 27 '22 03:10

Giulio Vian