Configures an S3 bucket to send notifications to an SNS topic when new objects are created with a .log suffix.

Terraform Template

data "aws_iam_policy_document" "topic" {

  statement {
    actions = ["SNS:Publish"]

    condition {
      test = "ArnLike"
      values = [aws_s3_bucket.bucket.arn]
      variable = "aws:SourceArn"
    }
    effect = "Allow"

    principals {
      identifiers = ["s3.amazonaws.com"]
      type = "Service"
    }
    resources = ["arn:aws:sns:*:*:s3-event-notification-topic"]
  }
}

resource "aws_s3_bucket" "bucket" {
  bucket = "your-bucket-name"
}

resource "aws_s3_bucket_notification" "bucket_notification" {
  bucket = aws_s3_bucket.bucket.id

  topic {
    events = ["s3:ObjectCreated:*"]
    filter_suffix = ".log"
    topic_arn = aws_sns_topic.topic.arn
  }
}

resource "aws_sns_topic" "topic" {
  name = "s3-event-notification-topic"
  policy = data.aws_iam_policy_document.topic.json
}