AWS Builder Center

Building an Event-Driven Alert System with AWS Lambda and Amazon SNS

Learn how to create a serverless, event-driven architecture using AWS Lambda and Amazon SNS. This step-by-step guide covers setting up automated email notifications triggered by cloud events to build reliable monitoring workflows.

Introduction

Modern cloud architectures rely heavily on decoupled, event-driven workflows. In this tutorial, we will build a serverless notification system that automatically processes incoming events and alerts administrators via email when specific system conditions are met.

Architecture Overview

Our setup uses the following core services:
  • Amazon EventBridge: Moniters infrastructure changes or cron schedules.
  • AWS Lambda: Processes incoming data payload and determines if an alert is warranted.
  • Amazon SNS (Simple Notification Service): Dispatches immediate email notifications to subscribers.

Prerequisites

Before moving forward, ensure you have:
  1. An active AWS Account  with administrative access.
  2. The AWS CLI installed and authenticated on your local machine.

Step 1: Create the Amazon SNS Topic

First, we need a communication channel to handle the outgoing notifications.
  1. Navigate to the Amazon SNS Console.
  2. Click Topics -> Create topic.
  3. Select Standard, name it SystemAlertsTopic, and click Create topic.
  4. Inside your new topic, click Create subscription. Select Email as the protocol, enter your email address, and click Create subscription.
Note: Check your inbox and click the validation link inside the AWS confirmation email to activate the subscription.

Step 2: Deploy the AWS Lambda Function

Next, we will deploy the computing layer that evaluates incoming telemetry data.
Create a file named lambda_function.py and add the following Python code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import json
import boto3
import os

sns_client = boto3.client('sns')
SNS_TOPIC_ARN = os.environ.get('SNS_TOPIC_ARN')

def lambda_handler(event, context):
print("Received event: " + json.dumps(event))

# Simple logic to check for a high-risk system flag
if event.get('status') == 'CRITICAL':
message = f"Alert! Critical system status detected. Details: {event.get('detail')}"

response = sns_client.publish(
TopicArn=SNS_TOPIC_ARN,
Message=message,
Subject="Immediate Action Required: AWS Cloud Alert"
)
return {"statusCode": 200, "body": "Alert sent successfully!"}

return {"statusCode": 200, "body": "Status normal. No alert sent."}

Step 3: Configure Environment Variables & IAM Permissions

To let Lambda talk to SNS securely, we must attach the appropriate IAM execution policies:
  1. In the Lambda function settings, go to the Configuration tab -> Permissions.
  2. Click on the execution role name to open the IAM Console.
  3. Attach the AmazonSNSFullAccess managed policy (or create a scoped down custom policy allowing sns:Publish on your specific Topic ARN).
  4. Go back to the Lambda Configuration tab -> Environment variables, add a key named SNS_TOPIC_ARN, and paste your actual SNS Topic Amazon Resource Name as the value.

Testing the System

You can test the entire workflow directly in the Lambda Console by configuring a test event with the following payload:
1
2
3
4
{
"status": "CRITICAL",
"detail": "Database CPU utilization has crossed 95% threshold."
}
Click Test. Within seconds, you should receive a direct email alert detailing the database metrics!

Conclusion

You have successfully built an event-driven notification engine using AWS serverless tools. From here, you can extend this architecture by hooking it up to CloudWatch Alarms or third-party webhooks to monitor complex global application states.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article