The Context: let me start saying I know "flat is better than nested" but in this case using flat variable structure lacks of semanatics so I created a nested structure as follows:
users-config = [
{
team = "team1"
custom-policies = [
{
policy-name = "p1"
policy-json = "<some long text in json>"
},
{
policy-name = "p2"
policy-json = "<some long text in json>"
}
]
groups = [
{
group-name = "g1"
policies-attached = ["p1", "p2"]
}
]
users = [
{
name = "u1"
user-name = "user.name"
email = "user-name@example.com"
groups = ["g1"]
}
]
}
]
This shows the policies, groups and users created for a specific team.
The Challenge: Creating this structure in terraform variable definition is quite simple, basically (many fields ommited):
variable "users-configf" {
type = list(object(
{
team = string
custom-policies = list(object({...}))
groups = list(object({...}))
users = list(object({...}))
}
))
}
But I have been using different combination of for_each / for expressions and I have not achieved traverse over this structure to create the resources. Basically I will pick some pieces from the structure to create the resources (at least that's my intention obviously code is not working):
resource "aws_iam_group" "test-groups" {
for_each = [for uc in var.users-config: for g in uc.groups: g.group-name]
name = each.value
}
The Expectation: I would traverse the structure and create the resources as needed (groups, policies, users, user-group membership and policies-groups attachments)
As I mentioned at the begining of the question, I can achieve this by creating flat variable definitions for users, groups, policies and creating the bindings between them but I'm looking for a descriptive variable. Any suggestion is welcome, thanks in advance...