DEV Community

Cover image for Deploying an S3 Event Notification to SNS topic using Terraform
Chinmay Tonape
Chinmay Tonape

Posted on

Deploying an S3 Event Notification to SNS topic using Terraform

In this post, we'll explore how to configure AWS S3 event notifications to send emails using SNS (Simple Notification Service) for S3 events. We'll leverage Terraform to automate the entire process.

Architecture Overview

Before we get started, let's take a quick look at the architecture we'll be working with:

Architecture Diagram

Step 1: Create an S3 Bucket

First, we need to create an S3 bucket where we will enable event notifications. Here is the Terraform code to create an S3 bucket:

####################################################
# S3 static website bucket
####################################################
resource "aws_s3_bucket" "my-bucket" {
  bucket = var.bucket_name
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Create an SNS Topic with Email Subscription

Next, we'll create an SNS topic and set up an email subscription to receive notifications. Below is the Terraform configuration for creating the SNS topic and the email subscription. Policy allows S3 to Publish events to SNS.

####################################################
# Create an SNS topic with a email subscription
####################################################
resource "aws_sns_topic" "s3-event-notification-topic" {
  name   = "s3-event-notification-topic"
  policy = <<POLICY
{
  "Version":"2012-10-17",
  "Statement":[{
    "Effect": "Allow",
    "Principal": { "Service": "s3.amazonaws.com" },
    "Action": "SNS:Publish",
    "Resource": "arn:aws:sns:us-east-1:197317184204:s3-event-notification-topic",
    "Condition":{
        "StringEquals":{"aws:SourceAccount":"197317184204"},
        "ArnLike":{"aws:SourceArn":"${aws_s3_bucket.my-bucket.arn}"}

    }
  }]
}
POLICY
}

resource "aws_sns_topic_subscription" "topic-email-subscription" {
  count     = length(var.email_address)
  topic_arn = aws_sns_topic.s3-event-notification-topic.arn
  protocol  = "email"
  endpoint  = var.email_address[count.index]
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Create S3 Event Notifications

Finally, we configure the S3 bucket to send event notifications to the SNS topic. The following Terraform code snippet demonstrates how to set up S3 event notifications:

####################################################
# Creating Bucket Event Notification 
####################################################
resource "aws_s3_bucket_notification" "bucket-notification" {
  bucket = aws_s3_bucket.my-bucket.id
  topic {
    topic_arn = aws_sns_topic.s3-event-notification-topic.arn
    events    = ["s3:ObjectCreated:*"] # You can specify the events you are interested in
  }
}
Enter fullscreen mode Exit fullscreen mode

Steps to Run Terraform

Follow these steps to execute the Terraform configuration:

terraform init
terraform plan 
terraform apply -auto-approve
Enter fullscreen mode Exit fullscreen mode

Upon successful completion, Terraform will provide relevant outputs.

Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
Enter fullscreen mode Exit fullscreen mode

Testing

S3 Bucket with Event Notifications enabled

S3 Bucket

S3 Bucket Event Notifications

SNS Topic with email subscription (Confirm the subscription on email before performing any S3 activity)

SNS Topic with email subscription

File upload to create S3 event

File Upload to S3

Event notifications received via email

Email Notification

Cleanup

Remember to stop AWS components to avoid large bills.

terraform destroy -auto-approve
Enter fullscreen mode Exit fullscreen mode

Conclusion

We have successfully configured AWS S3 event notifications to send emails using an SNS subscription. This setup can be particularly useful for monitoring and alerting purposes.

Resources

AWS S3 Notifications https://docs.aws.amazon.com/AmazonS3/latest/userguide/EventNotifications.html

Github Link: https://github.com/chinmayto/terraform-aws-s3-event-notifications-sns

Top comments (0)