Automating S Event Notifications with AWS Lambda and SNS: Complete Guide

Listen to this Post

Event-driven architectures are highly effective for scalable cloud solutions, especially when leveraging AWS services like S3, Lambda, and SNS. By configuring S3 bucket event notifications, you can trigger automated workflows in response to file uploads, deletions, or modifications. AWS Lambda allows custom code execution, while SNS enables event fan-out via email, SMS, or other endpoints.

You Should Know:

1. Setting Up S3 Event Notifications

To configure S3 bucket events via AWS CLI:

aws s3api put-bucket-notification-configuration \
--bucket your-bucket-name \
--notification-configuration file://notification-config.json

Example `notification-config.json`:

{
"LambdaFunctionConfigurations": [
{
"Id": "LambdaTriggerOnUpload",
"LambdaFunctionArn": "arn:aws:lambda:us-east-1:123456789012:function:ProcessUpload",
"Events": ["s3:ObjectCreated:"]
}
]
}

2. Creating an AWS Lambda Function

Deploy a Python Lambda function to process S3 events:

import boto3

def lambda_handler(event, context):
s3 = boto3.client('s3')
for record in event['Records']:
bucket = record['s3']['bucket']['name']
key = record['s3']['object']['key']
print(f"New file uploaded: {key} in {bucket}")

3. Configuring SNS for Notifications

Use AWS CLI to create an SNS topic and subscribe:

aws sns create-topic --name S3UploadNotifications 
aws sns subscribe \
--topic-arn arn:aws:sns:us-east-1:123456789012:S3UploadNotifications \
--protocol email \
--notification-endpoint [email protected]

4. Linking S3 → Lambda → SNS

Modify the Lambda function to publish to SNS:

sns = boto3.client('sns')
sns.publish(
TopicArn='arn:aws:sns:us-east-1:123456789012:S3UploadNotifications',
Message=f"New file detected: {key}",
Subject="S3 Upload Alert"
)

5. Verifying the Setup

  • Upload a file to S3:
    aws s3 cp test.txt s3://your-bucket-name/
    
  • Check CloudWatch Logs for Lambda execution:
    aws logs tail /aws/lambda/ProcessUpload --follow
    

What Undercode Say

Automating S3 events with Lambda and SNS streamlines cloud workflows, reducing manual intervention. Key AWS commands to master:
– S3 Management:

aws s3 ls s3://your-bucket/ 
aws s3 rm s3://your-bucket/file.txt 

– Lambda Updates:

aws lambda update-function-code --function-name ProcessUpload --zip-file fileb://deploy.zip 

– SNS Testing:

aws sns publish --topic-arn "arn:aws:sns:us-east-1:123456789012:S3UploadNotifications" --message "Test Alert" 

For large-scale deployments, integrate CloudTrail for logging and EventBridge for advanced routing.

Expected Output:

  • S3 uploads trigger Lambda → SNS alerts.
  • CloudWatch logs confirm event processing.
  • Email/SMS notifications delivered via SNS.

Reference: Automating S3 Event Notifications with AWS Lambda and SNS

References:

Reported By: Darryl Ruggles – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 TelegramFeatured Image