Event-Driven Notification System using AWS Lambda and Amazon SNS
Topics: Lambda, SNS, CloudWatch, Event-Driven Architecture, Serverless
Overview
This lab demonstrates building an event-driven notification system using serverless computing. An event in JSON format is given as input to an AWS Lambda function. The Lambda function processes the event and generates a notification message. The message is published to an Amazon SNS topic. SNS sends the notification to the subscribed email address. The execution details are verified using CloudWatch Logs.
Key Concepts
| Concept | Description |
|---|---|
| Event-Driven Architecture | System that responds to events asynchronously |
| AWS Lambda | Serverless compute service for running code without managing servers |
| Amazon SNS | Simple Notification Service for sending messages to subscribers |
| CloudWatch Logs | Monitoring and logging service for AWS resources |
| JSON Event Processing | Handling structured data inputs in serverless functions |
| IAM Permissions | Managing access control for AWS services |
Prerequisites
- Active AWS account with billing enabled
- IAM permissions for Lambda, SNS, and CloudWatch
- Basic knowledge of Python and JSON
- Email address for SNS notifications
Architecture Overview
Click to expand Architecture Diagram
Phase A: Create SNS Topic + Email Subscription
Open SNS: AWS Console → Search SNS → Open Simple Notification Service.
Create a Topic: Click Topics → Create topic. Select Standard, name it
NotificationTopic, and click Create topic.Open the created topic and copy Topic ARN. You will need this for the Lambda code.
Create Email Subscription: Inside the topic, click Create subscription. Select Email as the protocol and enter your email address as endpoint. Create subscription
Confirm Subscription: Click the confirmation link in your email inbox. The status in SNS should become Confirmed.
Phase B: Create Lambda Function
Open AWS Lambda
Create function: Author from scratch, Name it
NotificationSimulator, use Python 3.11, and create a new role with basic permissions.Add Lambda Code in lambda_function.py: In the Code tab, paste the following (replace
<SNS_TOPIC_ARN>with your actual ARN) and click Deploy:
Lambda → Code tab → open lambda_function.py → remove existing code → paste:
Replace <SNS_TOPIC_ARN> with your copied Topic ARN.
lambda_function.py Code
import json
import boto3
sns = boto3.client('sns')
TOPIC_ARN = "<SNS_TOPIC_ARN>"
def lambda_handler(event, context):
print("Notification Simulator invoked")
if "eventType" not in event or "user" not in event:
return {
"statusCode": 400,
"body": json.dumps({"message": "Missing 'eventType' or 'user'"})
}
event_type = event["eventType"]
user = event["user"]
if event_type == "ORDER_PLACED":
message = f"Hi {user}, your order is placed successfully."
priority = "NORMAL"
elif event_type == "PAYMENT_FAILED":
message = f"Hi {user}, your payment has failed. Please retry."
priority = "HIGH"
elif event_type == "LOW_ATTENDANCE":
message = f"Hi {user}, your attendance is low. Please take action."
priority = "HIGH"
else:
message = f"Hi {user}, unknown event received."
priority = "LOW"
print("Event Type:", event_type)
print("Message:", message)
print("Priority:", priority)
# Publish to SNS
try:
sns.publish(TopicArn=TOPIC_ARN, Message=message, Subject=f"{event_type} [{priority}]")
except Exception as e:
print("Error publishing to SNS:", str(e))
return {
"statusCode": 500,
"body": json.dumps({"message": "Failed to send notification"})
}
return {
"statusCode": 200,
"body": json.dumps({
"EventType": event_type,
"Message": message,
"Priority": priority
})
}Phase C: Permissions and Testing
Attach SNS Publish Policy: Go to Configuration → Permissions → Click the Role name.
On the IAM page, click Add permissions → Attach policies and attach
AmazonSNSFullAccess. Now lambda can publish to SNSCreate a test event: Name it
PaymentFailedwith the following JSON: Lambda → Test tab → Create new test event
{
"eventType": "PAYMENT_FAILED",
"user": "Anita"
}- Run Test: Click Test. Check your email for a notification with the subject
PAYMENT_FAILED [HIGH]. Message: “Hi Anita, your payment has failed. Please retry.”
Phase D: Verify CloudWatch Logs
Lambda → Monitor → View CloudWatch logs
Open latest log stream.
Verify these prints exist:
- Notification Simulator invoked
- Event Type:
- Message:
- Priority:
Validation
Validation
Expected Outputs
- Lambda test status: Succeeded
- Email received from SNS with correct message
- CloudWatch logs showing event type and generated message
Cost Considerations
Cost Considerations
- Lambda: ~$0.20 per 1M requests + compute time (~$0.0000167/GB-second)
- CloudWatch Logs: ~$0.50/GB ingested
- SNS: ~$0.50/100,000 email notifications
- Tip: Lambda has generous free tier; delete functions after lab.
Cleanup
Cleanup
- Delete Lambda functions:
StudentGradeLoggerandNotificationSimulator. - Delete SNS Topic:
NotificationTopic. - Delete CloudWatch Log Groups associated with the functions.
- Delete the IAM roles if they are not used elsewhere.
Result
Successfully created serverless Lambda functions with business logic, logging, and SNS integration. Demonstrated event-driven processing, error handling, and monitoring in a serverless architecture.
Viva Questions
- What is event-driven architecture and how does Lambda support it?
- How does SNS enable decoupled communication between services?
- Why are CloudWatch Logs important for serverless functions?
- What are the benefits of serverless computing for notification systems?
Quick Start Guide
Quick Start Guide
- Create SNS topic and email subscription; confirm subscription via email.
- Create Lambda function with Python runtime and basic execution role.
- Add business logic code to process input events and publish to SNS.
- Create test events for different scenarios; run tests.
{
"eventType": "PAYMENT_FAILED",
"user": "Anita"
}- View execution logs in CloudWatch to verify outputs.
- Check email for SNS notifications.
