0
votes

I am creating a Terraform module that allows users to specify a map of s3 buckets and the properties of the event notifications they wish to add to those buckets.

The variable that they will pass to the module will look something like the following:

    input = {
    bucket_1 = {
        name            = "name-of-bucket-1"
        filters = [
            {
                name            = "filter1"
                filter_prefix   = "/test"
                filter_suffix   = ".txt"
            },
            {
                name            = "filter2"
                filter_prefix   = ""
                filter_suffix   = ".gz"
            }
        ]
    },
    bucket_2 = {
        name            = "name-of-bucket-2"
        log_source_type = "aws:cloudtrail:sandbox"
        filters = [
            {
                name            = "filter1"
                filter_prefix   = ""
                filter_suffix   = ".gz"
            }
        ]
    }
}

The resource block will be created as follows:

resource "aws_s3_bucket_notification" "notification" { 
  for_each    = var.input
  bucket      = each.value.name

  dynamic "topic" {
    for_each = each.value.filters
    content {
      topic_arn = aws_sns_topic.sns_topic_s3[each.key].arn
      events = ["s3:ObjectCreated:*"]
      filter_prefix = each.value.filters.filter_prefix
      filter_suffix = each.value.filters.filter_suffix
    }
  }
}

Unfortunately, when attempting to run a plan, I am getting the following error:

Error: Unsupported attribute

on modules/aws-splunk-forwarder-s3/main.tf line 26, in resource "aws_s3_bucket_notification" >"splunk_forwarder_s3": 26: filter_suffix = each.value.filters.filter_suffix |---------------- | each.value.filters is tuple with 2 elements

This value does not have any attributes.

Does anyone have idea how I can achieve this? Thanks, Adam

1

1 Answers

0
votes

Nevermind everyone... I managed to work it out. Posting my answer in case anybody else is in the same predicament.

resource "aws_s3_bucket_notification" "notification" { 
  for_each    = var.splunk_s3_input
  bucket      = each.value.name

  dynamic "topic" {
    for_each = [for s in each.value.filters: {
      suffix = s.filter_suffix
      prefix = s.filter_prefix
    }]
    content {
      topic_arn = aws_sns_topic. sns_topic_s3[each.key].arn
      events = ["s3:ObjectCreated:*"]
      filter_prefix = topic.value.suffix
      filter_suffix = topic.value.prefix
    }
  }
}

My issue was using "each.value" instead of "topic.value" when attempting to reference the values of the dynamic block loop.