AWS SNS Topic Subscription Example: A Step-by-Step Guide
Amazon Simple Notification Service (SNS) is a fully managed messaging service that enables the publishing of messages to subscribers through various protocols like email, SMS, HTTP/S, and Lambda functions. With SNS, you can build robust and scalable messaging systems for alerting, monitoring, and application integration.
This article provides a practical example of creating an SNS topic, publishing a message, and subscribing to it using email and HTTP.
What Is AWS SNS?
Amazon SNS provides:
- Message Publishing: Send messages to multiple subscribers.
- Protocol Support: Deliver messages via email, SMS, Lambda, HTTP/S, or SQS.
- Real-Time Notifications: Trigger notifications instantly for system events.
AWS SNS Topic Subscription Example
Objective
We’ll:
- Create an SNS topic.
- Subscribe to the topic using email and HTTP.
- Publish a message to test the setup.
Step 1: Create an SNS Topic
- Navigate to the AWS SNS Console:
- Go to AWS Management Console → Simple Notification Service → Topics → Create Topic.
- Configure the Topic:
- Type: Standard (or FIFO if message ordering is required).
- Name: Enter a descriptive name (e.g.,
ExampleTopic
).
- Create the Topic:
- Click Create Topic to complete the setup.
Step 2: Add Subscriptions
1. Subscribe via Email
- Select the created topic → Subscriptions → Create Subscription.
- Protocol: Choose
Email
. - Endpoint: Enter your email address.
- Confirm Subscription:
- Check your email inbox for the confirmation link.
- Click the link to confirm the subscription.
2. Subscribe via HTTP
- Set up an HTTP endpoint:
- Deploy a simple HTTP server or API endpoint to receive SNS messages.
- Subscribe the HTTP endpoint:
- Go to the topic → Subscriptions → Create Subscription.
- Protocol: Choose
HTTP
. - Endpoint: Enter your HTTP server URL (e.g.,
http://your-domain.com/sns
). - Confirm the subscription by processing the
SubscribeURL
from the initial SNS message sent to your HTTP endpoint.
Example using Flask (Python):
from flask import Flask, request
app = Flask(__name__)
@app.route('/sns', methods=['POST'])
def sns_handler():
print("Message received:", request.json)
return '', 200
if __name__ == '__main__':
app.run(port=5000)
Step 3: Publish a Test Message
- Navigate to the Topic:
- Select the topic and click Publish Message.
- Compose the Message:
- Subject: Test Message (for email protocols).
- Message Body: Enter the content of the message.
- Publish:
- Click Publish Message.
- Verify Delivery:
- Email: Check your inbox for the received message.
- HTTP: Confirm the message was received and logged by your HTTP server.
Step 4: Monitor the SNS Topic
- View Delivery Metrics:
- Use the SNS Metrics dashboard in CloudWatch to monitor message delivery status.
- Enable Logging:
- Configure logging for the topic to troubleshoot failed deliveries.
Best Practices for Using SNS
- Secure Your Topic:
- Use topic policies to restrict access to trusted publishers and subscribers.
- Enable Dead-Letter Queues (DLQ):
- Capture undeliverable messages for debugging and analysis.
- Monitor Usage:
- Use CloudWatch to track message throughput and delivery failures.
- Optimize Protocols:
- Choose protocols based on use cases (e.g., Lambda for serverless workflows, SQS for message queuing).
Conclusion
Amazon SNS provides a scalable and flexible platform for building notification systems. By following this example, you can create an SNS topic, add email and HTTP subscriptions, and publish messages. AWS SNS is an essential tool for enabling real-time communication and integration in cloud-based applications.