Automating S3 File Upload Email Notifications with AWS Lambda and SNS
Published: 2023-10-21 02:28:23

**Introduction**
In the fast-paced world of cloud computing, automation is the key to efficiency and reliability. One common scenario is the need to receive notifications whenever files are uploaded to an Amazon S3 bucket. In this blog post, we'll explore how to set up an AWS Lambda function to automatically send SNS notifications whenever a file is uploaded to an S3 bucket.

**Prerequisites**
Before we dive into the technical details, here are some prerequisites to get started:

1. An AWS account.
2. An S3 bucket where you will upload files.
3. An SNS topic for sending notifications.
4. Basic knowledge of AWS services and Python.
5. Setting Up the AWS Lambda Function
6. Creating the Lambda Function


To create an AWS Lambda function in Python that sends a push notification using Amazon SNS whenever a file is uploaded to an S3 bucket, you can follow these steps. 

**Create an S3 Bucket**:

   You can follow on-screen guidance to create an S3 bucket. After successfully creating the bucket, make a note of the bucket name. You will need to update the lambda function code with this bucket name.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dljttdpwiqt7nmc2089c.png)


Bucket Name: s3-lambda-sns1
AWS Region : US East(N. Virginia) us-east-1 (_Note_, _your Lambda,SNS services should be part of same region_)

<u>The rest of the options should remain as their default settings, there is no need for any additional changes.</u>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ankt6znyw0pojogkurgf.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/77jxw6s3plkekw27usb0.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jqxvd47mywm2wpvdwloa.png)


Click on _Create Bucket_ to create the bucket.




**Create an SNS Topic**:

   Create an SNS(Simple Notification Service) topic that will be used to send notifications. Note the topic ARN to update in lambda code [YOUR_SNS_TOPIC_ARN].


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tbbac3dvcrbhcdvgmvip.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/98xay52cdrvzeexrme4n.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l59ybfsi1t885i5oxf2h.png)



<u>The rest of the options should remain as their default settings, there is no need for any additional changes.</u>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5nunl61mr2gqsb6xb83e.png)

_SNS is services created._

create Subscription to receive the email alerts

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5srtxxdiq20tbz0vkw09.png)

> Note: Once you enter your email address, Amazon will send a confirmation email to the provided address. you should verify it, as email alerts will not be triggered until the email address is verified.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hqg62yng90undhvrsaem.png)



**Create an IAM Role**:

   Create an IAM role for your Lambda function that allows it to interact with S3 and SNS. Attach policies for `AmazonS3FullAccess` and `AmazonSNSFullAccess`.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ws5u8rtavgkokay8vq9.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8y1o5w3gylwcpvwi5ej6.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e4lhozf6x9estamfmq17.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ci8l8kte10c878xlj5p.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sd323hlatc7s2sh8oh65.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93gluyut3lymy73wsxml.png)







**Create the Lambda Function**:

   Create a Lambda function with the following Python code.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8u0my78yz3uz4jdiyptn.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u3145rrydktwnm8i220t.png)




![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/li1s43h1f43syj3lpghu.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ke5ybf48mlo9aplz169r.png)





`code for your reference:`

```python
import json
import boto3

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

def lambda_handler(event, context):
    # Retrieve information about the S3 event
    records = event.get('Records', [])

    for record in records:
        if record['eventName'] == 'ObjectCreated:Put':
            # Extract the bucket and object key from the S3 event
            bucket_name = record['s3']['bucket']['name']
            object_key = record['s3']['object']['key']
# S3 trigger is added to the Lambda function, S3 values will be automatically loaded
            # Send a notification using SNS
            message = f"File '{object_key}' has been uploaded to the S3 bucket '{bucket_name}'."
            subject = "S3 File Upload Notification"
            
            sns.publish(
                TopicArn='YOUR_SNS_TOPIC_ARN',
                Subject=subject,
                Message=message
            )

    return {
        'statusCode': 200,
        'body': json.dumps('Notification sent successfully!')
    }
```



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i0f0foei4i0hcw6mhnjr.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ob32t0tuyamyp9h353mv.png)



**Testing**:

   You can test the Lambda function by uploading a file to the specified S3 bucket. You should receive an email notification through the SNS topic when the Lambda function is triggered.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5rn8wqh6k5972qlzd9pk.png)


<u>Email alert is triggered after file uploaded to S3</u>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8zmqpd9kde941ksa1r13.png)



Remember to set up proper error handling, logging, and security best practices in your Lambda function to ensure it works reliably in a production environment. Also, ensure that the IAM role assigned to the Lambda function has appropriate permissions for S3 and SNS.






Conclusion
In this blog post, we've demonstrated how to automate the process of sending SNS notifications whenever a file is uploaded to an S3 bucket using AWS Lambda. This approach is a valuable addition to your cloud automation toolkit, providing real-time alerts for file uploads.

By following the steps outlined in this blog post, you can easily set up your own S3 file upload notification system and adapt it to your specific use case. Automation not only saves time but also enhances the reliability and responsiveness of your AWS infrastructure.

Stay tuned for more AWS and cloud-related tips and tutorials!