AWS Email Notifications using SNS
I am writing the general steps that can be used to set up AWS Email Notifications using SNS. Please note that you will need your own AWS account and EC2 server instance to follow these steps hands on.
First of all, let me give description about what is AWS SNS and what it is used for:
Amazon Simple Notification Service (SNS) is a fully managed messaging service provided by Amazon Web Services (AWS). It allows you to send messages (notifications) to multiple subscribers through a "publish-subscribe" model. With SNS, you can decouple and scale microservices, distributed systems, and serverless applications.
Key Features:
Publish-Subscribe Model: SNS follows the pub-sub pattern where messages are sent by publishers to a topic, and then the messages are delivered to subscribers of that topic.
Multiple Protocols: SNS supports multiple messaging protocols including HTTP/HTTPS, email, SMS, and AWS Lambda.
High Throughput and Scalability: Designed to handle very large numbers of messages and subscribers, ensuring low latency and high availability.
Message Filtering: Allows subscribers to receive only the messages they are interested in based on message attributes.
Security: Provides access control through AWS Identity and Access Management (IAM) and supports encryption for data at rest and in transit.
Use Cases of AWS SNS
Application Integration:
Decoupling Microservices: SNS enables communication between microservices in a decoupled fashion, allowing each service to operate independently.
Event Notification: Trigger events and notifications across distributed systems and applications.
User Notifications:
Mobile Push Notifications: Send notifications to mobile devices using services like Amazon Pinpoint or integrating with Firebase Cloud Messaging (FCM) and Apple Push Notification Service (APNs).
Email/SMS Alerts: Send real-time alerts and notifications to users via email or SMS for various purposes like marketing, security alerts, or updates.
Monitoring and Incident Management:
Operational Alerts: Integrate with monitoring systems (like Amazon CloudWatch) to receive alerts for system performance, security incidents, or other critical events.
Health Checks: Automatically notify relevant teams or systems when certain thresholds are crossed or anomalies are detected.
Log Processing and Data Pipelines:
Data Ingestion: Integrate with data pipelines to process and route logs or data from various sources to destinations like AWS Lambda, S3, or databases.
Streaming Data: Forward data to various consumers for real-time processing and analytics.
IoT (Internet of Things):
Device Communication: Send notifications from IoT devices to users or other systems, facilitating real-time data transfer and alerts.
Fleet Management: Manage large fleets of IoT devices by sending and receiving updates and commands.
Workflow Automation:
Trigger Lambda Functions: Use SNS to trigger AWS Lambda functions for serverless processing and automation.
Orchestrate Distributed Systems: Coordinate complex workflows by passing messages and events between different parts of the system.
Third-Party Integrations:
Cross-Service Communication: Use SNS to integrate with other AWS services (like SQS, Lambda, etc.) or third-party services for extended functionality.
API Integration: Enable external systems to publish messages to SNS topics via APIs for real-time communication.
Hands On practical steps to set up AWS SNS are the following:
- Open the CloudWatch console by searching CloudWatch in the Search bar
- Click on the Log group named
dev-web-mrkt-001 by selecting Log Group Name from the left navigation
- Select the Metrics Filter tab from the bottom after clicking on log group
- Select the apache-stopped-metric from Metric Filter tab.
Click on the Metric named
dev-web-mrkt-001 > Metrics with no dimensions
Tick the box next to the Metric named
apache-stopped
- Click on the Graphed Metrics tab at the bottom
- Click on the Bell icon to create an Alarm
Select Maximum for the statistic and 1 minute for the Period in the Specify metrics and conditions page
In the conditions section, leave the default of Threshold type Static and default for the alarm condition as Greater. Enter 0 for the threshold value.
- On the notification configuration, Leave the default of
In Alarm
for the Alarm state trigger. Change the SNS topic option toCreate new topic
. Give the new SNS topic the name ofmarketing-web-alerts
andprovide your email
in the Endpoints that will receive the notification box.
- Click the Create topic button to create the SNS topic that will be used to send SNS notifications.
13. Provide name of alarm in the text box. Call it Apache Stopped.
On the Next page, Preview and click on Create Alarm
Go to your email Inbox and confirm the SNS Subscription by clicking the Confirm Subscription link in the email.
Go back to the CloudWatch, let's look at the alarm. If you don't see it, click on
All alarms
in the navigation pane on the left. If the alarm state shows Insufficient Data, we will need to manually push some data to CloudWatch.
Browse to CloudShell service by typing CloudShell in the search bar and selecting the
CloudShell
service.Use the following command to override the current state of the alarm
aws cloudwatch set-alarm-state --alarm-name Apache-Stopped --state-value OK --state-reason testing --region us-east-1
Back in the CloudWatch console, browse to the alarm and validate the state now shows
OK
Let's validate that our alarm and email notifications are working as expected. Browse back to the EC2 console and establish a terminal session to the
dev-web-mrkt-001
instance.Trigger the alarm by stopping the Apache service using the command
sudo systemctl stop httpd
Back in the CloudWatch alarms console, see if the alarm has been triggered. It will display In Alarm as the state in red letters. Note that it could take up to 5 minutes for CloudWatch Alarm to trigger.
Check your email and validate you received the new notification that the Apache service has been stopped on the server.
Conclusion
AWS SNS is a powerful tool for building scalable, decoupled, and event-driven architectures. Its flexibility and support for multiple protocols make it suitable for a wide range of applications, from simple user notifications to complex microservice communication and IoT solutions.