Greetings.
I’m trying to create a users groups from a map of list.
This is my files.
tfe.auto.tfvars
# Groups
groups = [
{
name = "admins"
members = ["admin1@example.com","admin2@example.com"]
},
{
name = "users"
members = ["user1@example.com"]
}
]
main.tf
module "groups" {
source = "./modules/groups"
groups = var.groups
}
/modules/groups/data.tf
data "azure_client_config" "current" {}
data "azuread_user" "readusers" {
for_each = { for i, group in var.groups : group.name => group }
user_principal_name = each.value.members[0]
}
/modules/groups/variables.tf
variable "groups" {
type = list(object({
name = string
members = list (string)
}))
}
/modules/groups/main.tf
resource "azuread_group" "example" {
for_each = { for i, group in var.groups : group.name => group }
display_name = "test"
owners = [data.azuread_client_config.current.object_id]
security_enabled = true
members = [
data.azuread_user.readusers[each.key].object_id,
]
}
The mainly problem is in te data source
data "azuread_user" "readusers" {
for_each = { for i, group in var.groups : group.name => group }
user_principal_name = each.value.members[0]
}
The property user_principal_name doesn’t accepts list, only strings.
Tried multiples situations but doesn’t works.
Whats wrong with this?
Reallly appreaciete any help
Thank you for your detailed presentation of your existing code, including full file paths, correctly enclosed in code blocks so that it is nicely readable . It is so refreshing to see a post in these forums where the asker has made things easier for potential answerers.
The important point here is to consider what you actually want to be iterating over in the data source. Since you want to look up each user once, even if the same user occurs in multiple groups, a set of user names is ideal: