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

In main.tf
I used below code to create subscription in Azure ENV

data "azurerm_billing_enrollment_account_scope" "example" {
  billing_account_name    = "1234567890"
  enrollment_account_name = "0123456"
}

resource "azurerm_subscription" "example" {
  subscription_name = "My Example EA Subscription"
  billing_scope_id  = data.azurerm_billing_enrollment_account_scope.example.id
}

in code when I try to add management group, using
resource "azurerm_management_group_subscription_association" "example" {
  management_group_id = data.azurerm_management_group.example.id
  subscription_id     = resource.azurerm_subscription.example.id
}
I am getting error that cannot parse subscription with ID **.

I tried 
  subscription_id     = resource.azurerm_subscription.example.id
  subscription_id     = resource.azurerm_subscription.example.subscription_id
  subscription_id     = azurerm_subscription.example.id
  subscription_id     = azurerm_subscription.example.subscription_id

used data blocks also but not successful. Do you know what I am missing

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