Hello HashiCommunity,
I am wondering if anybody would be aware of a way to iterate through a map in the templatefile function directly (nested in a for_each).
Let me explain by giving the case which I am currently working on.
I aim to create an AWS SNS module that generates some SNS topics and attaches an access policy to them.
Here’s what I got so far:
MAIN.TF
resource "aws_sns_topic" "sns_topics" {
for_each = var.sns_topics /* A map that contains the names of the topics to be created */
name = each.value.name
}
resource "aws_sns_topic_policy" "sns_topic_policy" {
for_each = var.sns_policies /* A map that contains the same key names as the ones in sns_topics */
arn = aws_sns_topic.sns_topics[each.key].arn
policy = templatefile(each.value.filepath,{
aws_account_id = var.aws_account_id
topic_name = each.value.topic_name
policy_id = each.value.policy_id
sid = each.value.variables["access_policy"].sid
effect = each.value.variables["access_policy"].effect
action = each.value.variables["access_policy"].action
resource = each.value.variables["access_policy"].resource
[...]
}
)
}
Since a single access policy can contain multiple sets of sid, effect, action, resource, …,
I understand that targeting a specific key in the map each.value.variables is not the best way to achieve what I am looking for.
I am looking for a way (maybe a wildcard character or a Terraform trick that I would not be aware of) to iterate through each.value.variables for_each keys that it contains.
Any tips would be greatly appreciated.
Let me know if you need any other information regarding the case.
Thank you !