data source aws_iam_policy
returns an object with attrubutes about a policy, passing a policy arn. Using a configuration like this:
locals{
maps_of_policy_arns = {
managed1 = {
pol1 = "arn:aws:iam::aws:policy/IAMFullAccess", # Allow IAM resource management
pol2 = "arn:aws:iam::aws:policy/AmazonEC2FullAccess", # Allow EC2 resource management
pol3 = "arn:aws:iam::aws:policy/AmazonS3FullAccess", # Allow S3 resource management
}
managed2 = {
pol1 = "arn:aws:iam::aws:policy/AmazonRDSFullAccess", # Allow RDS management
pol2 = "arn:aws:iam::aws:policy/AWSDirectoryServiceFullAccess", # Allow DirectoryService management
}
}
}
data "aws_iam_policy" "managed1" {
for_each = local.maps_of_policy_arns.managed1
arn = each.value
}
data "aws_iam_policy" "managed2" {
for_each = local.maps_of_policy_arns.managed2
arn = each.value
}
I can get e.g. from data.aws_iam_policy.managed1
a map of objetcs like this (I’m interested to inline policy part - policy
) :
{
"pol1" = {
"arn" = "arn:aws:iam::aws:policy/IAMFullAccess"
...
"policy" = <<-EOT
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["iam:*",
"organizations:DescribeAccount",
...
"Resource": "*"
}
]
}
EOT
}
"pol2" = {
"arn" = "arn:aws:iam::aws:policy/AmazonEC2FullAccess"
...
"policy" = <<-EOT
{
...
EOT
...
}
I’d like to avoid static behavior imposing to create a data source for each map of policy arns (e.g. managed1, managed2, managedN), making more dynamic and DRY code, but as this data source returns a single object
I cannot go over a map of objects
using for_each.
It is possible to get a maps og objects like this ?
maps_of_policy_documents = {
managed1 = {
"pol1" = {
"arn" = "arn:aws:iam::aws:policy/IAMFullAccess"
...
"policy" = <<-EOT
...
EOT
}
"pol2" = {
"arn" = "arn:aws:iam::aws:policy/AmazonEC2FullAccess"
...
"policy" = <<-EOT
...
EOT
}
...
}
managed2 = {
"pol1" = {
...
}
...
}
}
e.g. using some dynamic approach like flatten([…]) and local for loop with expression (without using static filtering condition on key name etc.) ?