Using a for_each on a data block

I am trying to use a foreach in my data block to get the members of each azuread_groups

lz_subscription = {
  identity = {
  subscription_display_name = " Identity"
  subscription_name         = "Identity"
  subscription_alias_name   = "Identity"
  subscription_workload     = "Production"
  azuread_groups = {
  SupportRequestContrib = {
    name             = "SupportRequestContrib"
    security_enabled = "true"
    role             = "Support Request Contributor"
    members = [
      "Elevated-Cloud"
    ]
  }

my data block

data "azuread_groups" "member_groups" {

}

I’m running into problems trying to figure out how to get inside of the azuread_groups part and get only the members.

Hi @dcwbys276,

I would use azuread_group instead of azuread_groups with an ‘s’ but first thing is to create a locals in other to enumerate on each of the groups in your lz_subscription object of object. See what the locals and the data block would look below.

locals {
  azuread_groups = flatten(
    [for a, b in var.lz_subscription :
      [for x, y in b.azuread_groups :
        {
          subcription        = b.name
          name               = y.name
          security_enabled   = y.security_enabled
          role               = y.role
          members            = y.members
        }
      ]
    ]
  )
}
data "azuread_group" "member_group" {
  foreach = {
    for group in local.azuread_groups: "${group.subscription}|${group.name}" => group
  }
  display_name       = each.value.name
  security_enabled   = each.value.security_enabled
}

Please let me know if this helps.