Hello,
I have been pulling my hair on this for a couple of days now, I was wondering if someone could come up with a clever way to produce something like:
Outputs:
association-map = {
"policy1_user1" = [
"policy1",
"user1"
]
"policy2_user1" = [
"policy2",
"user1"
]
"policy2_user2" = [
"policy2",
"user2"
]
}
From:
variable iam-policy-users-map {
default = {
"policy1" = [ "user1" ]
"policy2" = [ "user1", "user2" ]
}
}
The actual use case behind this is that I would like my team to be able to define GCP roles and members in two distinct lists to begin with:
variable admin_roles = {
default = [
"roles/resourcemanager.folderAdmin",
"roles/resourcemanager.folderIamAdmin",
]
}
variable admin_members = {
default = [
"user:user1@example.com",
"group:group1@example.com",
]
}
I then create a bindings map out of the two lists and feed it to a google_folder_iam_binding resource with for_each:
locals {
admin_bindings = {
for role in var.admin_roles:
role => var.admin_members
}
}
resource "google_folder_iam_binding" "binding" {
for_each = local.admin_bindings
folder = "folder_1234"
members = each.value
role = each.key
}
Which works as intended and allows us to remove/add members and roles anywhere in the original lists without triggering a delete/create of the resulting resources.
But I then realized I did not always want to be authoritative on the roles bindings, so I embarked on trying to produce a map I could feed to a google_folder_iam_member resource with for_each:
locals {
admin_bindings_additive = {
# insert magic to transform local.admin_bindings into a map of maps linking
# each role with each of its member in the following format:
# "roles/resourcemanager.folderAdmin_user:user1" = [
# "roles/resourcemanager.folderAdmin",
# "user:user1@example.com"
# ]
# "roles/resourcemanager.folderAdmin_group:group1@example.com" = [
# "roles/resourcemanager.folderAdmin",
# "group:group1@example.com"
# ]
# "roles/resourcemanager.folderIamAdmin_user:user1@example.com" = [
# "roles/resourcemanager.folderIamAdmin",
# "user:user1@example.com"
# ]
# "roles/resourcemanager.folderIamAdmin_group:group1@example.com" = [
# "roles/resourcemanager.folderIamAdmin",
# "group:group1@example.com"
# ]
# }
}
}
resource "google_folder_iam_member" "member" {
for_each = local.admin_bindings_additive
folder = "folder_1234"
member = each.value[1]
role = each.value[0]
}
But failed miserably, for obvious reasons described in https://github.com/hashicorp/terraform/issues/22263.
I am not ready to give up yet, so I am hoping someone smarter could point me in a better direction.