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:
- An AWS account.
- An S3 bucket where you will upload files.
- An SNS topic for sending notifications.
- Basic knowledge of AWS services and Python.
- Setting Up the AWS Lambda Function
- 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.
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)
The rest of the options should remain as their default settings, there is no need for any additional changes.
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].
The rest of the options should remain as their default settings, there is no need for any additional changes.
SNS is services created.
create Subscription to receive the email alerts
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.
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
.
Create the Lambda Function:
Create a Lambda function with the following Python code.
code for your reference:
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!')
}
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.
Email alert is triggered after file uploaded to S3
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!
Top comments (0)