Not getting Azure subscription ID in terraform code after creating new subscription

In my case, I had to use the local-exec to reload the subscription data, or it won’t ‘see’ the new subscription. Essentially when you login, it sees all the subscriptions at that point in time, so you’ll need to refresh/reload the subscription data to see the new sub.

See How to create Azure Subscription and deploy into it in same stream for how I used local-exec

For management groups, I used this code from within a module.

data "azurerm_management_group" "client" {
 name = "clients"
}
data "azurerm_subscription" "current" {
 subscription_id = azurerm_subscription.ow.subscription_id
}
resource "azurerm_management_group_subscription_association" "client" {

 management_group_id = data.azurerm_management_group.client.id
 subscription_id     = data.azurerm_subscription.current.id
}
1 Like