Hello I'm trying to convert a set of bucket notifications defined as follows to a map: sequence structure so I can iterate over the sequence with a dynamic block in my bucket notifications.
buckets_with_events = [
{
"bucket" = "bucket1"
"events" = [
"s3:ObjectCreated:*",
]
"filter_prefix" = tostring(null)
"filter_suffix" = ".xml"
"function" = "lambda1"
},
{
"bucket" = "bucket1"
"events" = [
"s3:ObjectCreated:*",
]
"filter_prefix" = tostring(null)
"filter_suffix" = ".csv"
"function" = "lambda1"
},
{
"bucket" = "bucket2"
"events" = [
"s3:ObjectCreated:*",
]
"filter_prefix" = tostring(null)
"filter_suffix" = ".csv"
"function" = "lambda1"
}]
I need to have the buckets as a unique key and have all events consolidated as a single sequence under that key. I have been trying the following, but I can't seem to correctly assign the tuple/set/sequence to the map key.
bucket_events = flatten([
for parent_obj in local.buckets_with_events:
parent_obj.bucket => [
for obj in local.buckets_with_events:
{events = obj.events
filter_suffix = obj.filter_suffix} if parent_obj.bucket == obj.bucket
]
])
The output I want to achieve is the following:
bucket_events = {
"bucket1" = [{
"events" = [
"s3:ObjectCreated:*",
]
"filter_prefix" = tostring(null)
"filter_suffix" = ".xml"
"function" = "lambda1"
},
{
"events" = [
"s3:ObjectCreated:*",
]
"filter_prefix" = tostring(null)
"filter_suffix" = ".csv"
"function" = "lambda1"
}]
"bucket2" = [{
"events" = [
"s3:ObjectCreated:*",
]
"filter_prefix" = tostring(null)
"filter_suffix" = ".csv"
"function" = "lambda1"
}]
In this way, I can easily generate all my bucket notifications that are required using for_each and dynamic as below --
resource "aws_s3_bucket_notification" "dynamic_s3_triggers" {
for_each = var.bucket_events
bucket = each.key
dynamic "lambda_function" {
content {
lambda_function_arn = aws_lambda_function.functions[lambda_function.function].function].arn
...
}
}
}
buckets_with_events?