Hello,
I am attempting to solve an issue creating a module with an s3 event trigger for sqs queue. First I use a list variable of var.s3_bucket_folders that defaults to an empty list with for_each to setup aws_s3_bucket_object. Then I need to create aws_s3_bucket_notification for_each and use a dynamic block so I can configure each aws_s3_bucket_object separately and set the proper filter_prefix.
The issue I have is that in the event that var.s3_bucket_folders is not provided I need to configure the same dynamic block for one instance and remove the filter_prefix. This is what I finally came up with right as I created this, struggled to find a working solution. But it still feels like a hack and not very readable.
resource "aws_s3_bucket_notification" "s3_bucket_notification" {
bucket = aws_s3_bucket.s3_bucket.id
dynamic "queue" {
for_each = {
for f in var.s3_bucket_folders : f => f
if length(var.s3_bucket_folders) != 0
}
content {
events = [
"s3:ObjectCreated:*"
]
queue_arn = var.sqs_arn
filter_suffix = ".json"
filter_prefix = queue.value
}
}
dynamic "queue" {
for_each = {
for f in ["no_prefix"] : f => null
if length(var.s3_bucket_folders) == 0
}
content {
events = [
"s3:ObjectCreated:*"
]
queue_arn = var.sqs_arn
filter_suffix = ".json"
filter_prefix = queue.value
}
}
}
Did I solve this in the correct way or is there something cleaner I can do to implement this?