I am trying to use data source "aws_iam_arn"
to retrieve user arns and assemble them to a local variables (a list) so it can be used to provision other aws resources. However I am stuck at pick the arn in the returned result
Here is my code
data "aws_iam_user" "this" {
for_each = { for user in local.users : user => user }
user_name = each.value
}
locals {
arn_list = [for user in data.aws_iam_user.this[*].arn : user]
users = ["user1", "user2"]
}
output "data_result" {
value = data.aws_iam_user.this[*]
}
output "user_arn" {
value = local.arn_list
}
the output data_result showing something as below
+ data_result = [
+ {
+ user1 = {
+ arn = "arn:aws:iam::zzzzzzzzzz:user/user1"
+ id = "AAAAAAAAAAAAAAAAAAA"
+ path = "/"
+ permissions_boundary = ""
+ tags = {
+ AAAAAAAAAAAAAAAAAAA = "user1"
}
+ user_id = "AAAAAAAAAAAAAAAAAAA"
+ user_name = "user1"
}
+ user2 = {
+ arn = "arn:aws:iam::zzzzzzzzzz:user/user2"
+ id = "BBBBBBBBBBBBBBBBBB"
+ path = "/"
+ permissions_boundary = ""
+ tags = {}
+ user_id = "BBBBBBBBBBBBBBBBBB"
+ user_name = "user2"
}
},
]
but getting below error
Error: Invalid index
│
│ on main.tf line 11, in locals:
│ 11: arn_list = [for user in data.aws_iam_user.this[0].arn : user]
│ ├────────────────
│ │ data.aws_iam_user.this is object with 2 attributes
│
│ The given key does not identify an element in this collection value. An
│ object only supports looking up attributes by name, not by numeric
│ index.