Hello
I want to add Azure AD group to the built-in Azure AD role in multiple subscriptions. Created below code:
locals {
multi_subs = [
"sub1",
"sub2",
.
.
.
"subX"
]
}
data "azurerm_subscription" "subs" {
for_each = toset(local.multi_subs)
}
resource "azurerm_role_assignment" "example" {
for_each = data.azurerm_subscription.subs
scope = data.azurerm_subscription.validation.id
role_definition_name = "User Access Administrator"
principal_id = "xxxxxxxxx" - ID of Azure AD users group
}
after terraform plan i got the following error:
Error: Reference to undeclared resource
│
│ on main.tf line 22, in resource “azurerm_role_assignment” “example”:
│ 22: scope = data.azurerm_subscription.validation.id
│
│ A data resource “azurerm_subscription” “validation” has not been declared in the root module.
I’m not sure how to handle it or if it’s good way of doing this task in that way in terraform
For the data source you aren’t specifying subscription_id which according to the docs means it will use the subscription ID of the current Azure Resource Manager provider. So while you are having lots of instances of that data source I’d expect them to all have identical values.
│ Error: retrieving Subscription (Subscription: “xxx”): subscriptions.Client#Get: Failure responding to request: StatusCode=400 – Original Error: autorest/azure: Service returned an error. Status=400 Code=“InvalidSubscriptionId” Message=“The provided subscription identifier ‘xxx’ is malformed or invalid.”
│
│ with data.azurerm_subscription.subs[“xxx”],
│ on main.tf line 33, in data “azurerm_subscription” “subs”:
│ 33: data “azurerm_subscription” “subs” {