Skip to main content

Command Palette

Search for a command to run...

Building an S3 Event-Driven AWS Lambda to Send Email Alerts and Backup Files

Updated
5 min read
Building an S3 Event-Driven AWS Lambda to Send Email Alerts and Backup Files

Introduction

In this blog, I’ll walk through a hands-on AWS serverless implementation where an S3 file upload automatically triggers a Lambda function, sends an email notification via SNS, and copies the uploaded object into a backup bucket.

This use case is common in real-world systems where teams need:

  • Immediate visibility when files are uploaded

  • Automated backups

  • Event-driven processing without managing servers


Problem Statement

Create an S3 event trigger to invoke a Lambda function that:

  • Sends an SNS email notification on every file upload

  • Copies the uploaded file into a backup S3 bucket

Email notification should include:

  • Source Bucket Name

  • AWS Region

  • File Name and File Size

  • Upload IP Address

  • Event Time


AWS Services Used

  • Amazon S3 – Object storage and event source

  • AWS Lambda – Serverless event processing

  • Amazon SNS – Email notification service

  • Amazon CloudWatch – Logs and monitoring


Key Concepts Practiced

Before implementing the solution, I practiced and understood the following Lambda fundamentals:

  • Handler – The entry point of the Lambda function

  • Event – JSON payload sent by S3 containing upload details

  • Context – Runtime metadata such as request ID and execution info

  • Test Event – Used for local validation (not used in final execution)

  • Environment Variables – Secure way to store configuration values


Architecture Overview

S3 (PUT Object Event)
Lambda Function
SNS Email Notification
Copy Object to Backup S3 Bucket

This architecture is fully event-driven and serverless.


Step 1: Configure S3 Event Trigger

An S3 bucket is configured to trigger a Lambda function only when a PUT (ObjectCreated) event occurs.

Concept used:

  • S3 Event Notifications allow S3 to notify other AWS services when specific actions occur.

Step 2: Create an SNS Topic and Subscribers

An SNS topic is created to send email notifications.
Email subscribers are added and confirmed.

Concept used:

  • SNS Topics act as a publisher-subscriber messaging system.

Step 3: Write the Lambda Function Code

The Lambda function performs the following tasks:

  • Reads file metadata from the S3 event

  • Formats a detailed email message

  • Publishes the message to SNS

  • Copies the uploaded object to a backup bucket

Lambda Code

import os,json,boto3

s3 = boto3.client("s3")
sns = boto3.client("sns")

def lambda_handler(event,context):
    print(event)
    source_bucket = event['Records'][0]['s3']['bucket']['name']
    aws_region = event['Records'][0]['awsRegion']
    key_val = event['Records'][0]['s3']['object']['key']
    size_val = event['Records'][0]['s3']['object']['size']/1024
    ipAddress = event['Records'][0]['requestParameters']['sourceIPAddress']
    event_time = event['Records'][0]['eventTime']
    message = "Hi, \n The Event time is : " + event_time + "Hi, \nYou are
receiving this email because you are subscribed to this S3event. \nThe Source
Bucket is : " + source_bucket + "\nThe AWS Region is :" + aws_region + "\nThe
Uploaded Filename is : " + key_val + " having Size : " + str(size_val) + " KB" +
"\nThe Object is upload from IP Address: " + ipAddress

 #Below are the variables for copy_object function parameters
 #Provide below the target bucket name where your object needs to be copied
    backupBucket = os.environ['BACKUP_BUCKET_NAME']
    snsArn = os.environ['SNS_TOPIC_ARN']
    emailSubject = "S3EventTrigger-Notification"
    copy_source = {'Bucket' : source_bucket, 'Key' : key_val}
    sns_response = sns.publish(TopicArn=snsArn,Message=message,Subject=emailSubject)
    print(sns_response)
    
    try:
        print("Copying the object from source to destination")
        s3.copy_object(Bucket=backupBucket,Key=key_val, CopySource=copy_source)
 
    except Exception as e:
        print(e)
        print("Error getting object")
        raise e

Concepts used:

  • Lambda Handler (lambda_handler)

  • Event Object (event['Records'])

  • Boto3 SDK for AWS service interaction


Step 4: Configure Environment Variables

Environment variables are added in Lambda to avoid hard-coding values.

Key Value
BACKUP_BUCKET_NAME <Backup_Bucket_Name>
SNS_TOPIC_ARN <SNS_Topic_ARN>

Concept used:

  • Environment Variables provide secure, reusable configuration.

Step 5: Upload File to Source S3 Bucket

A file is uploaded to the source S3 bucket.

This action automatically triggers the Lambda function.

Concept used:

  • Event-driven execution (no manual invocation)

Step 6: Verify Lambda Execution in CloudWatch

Lambda execution logs are verified in CloudWatch under:

/aws/lambda/<LambdaFunctionName>

Concept used:

  • CloudWatch Logs for monitoring and debugging serverless workloads

Step 7: Verify Backup Bucket

The uploaded file is successfully copied to the backup S3 bucket.


Step 8: Verify Email Notification

An email notification is received containing:

  • Source bucket name

  • AWS region

  • File name and size

  • Upload IP address

  • Event time


Conclusion

This project demonstrates a real-world AWS serverless workflow using S3, Lambda, and SNS.

It highlights how AWS services can be combined to build scalable, automated, and event-driven solutions without managing infrastructure.

Through this exercise, I gained hands-on experience with:

  • Lambda internals (handler, event, context)

  • S3 event notifications

  • SNS messaging

  • Environment-based configuration

  • CloudWatch monitoring

More from this blog

Sam's blog

56 posts