SNS

Amazon SNS
Author

Benedict Thekkel

Key Features of Amazon SNS

  1. Publish/Subscribe Model:
    • Allows publishers (producers) to send messages to topics.
    • Subscribers (consumers) receive messages from these topics in real-time.
  2. Message Delivery Channels:
    • HTTP/HTTPS Endpoints: Deliver messages to webhooks or custom services.
    • Amazon SQS: Integrate with SQS queues for asynchronous processing.
    • AWS Lambda: Trigger Lambda functions directly from SNS messages.
    • Email: Send messages as email notifications.
    • SMS: Send text messages globally to mobile numbers.
    • Mobile Push Notifications: Send push notifications to mobile devices via Firebase, APNs, or Baidu.
  3. Message Filtering:
    • Subscribers can define message filtering rules to receive only messages that meet specific criteria.
  4. Dead-Letter Queues (DLQs):
    • Messages that fail to deliver to subscribers can be routed to a dead-letter queue for debugging and troubleshooting.
  5. Cross-Account and Cross-Region Support:
    • Publish and subscribe across AWS accounts and regions for broader system architectures.
  6. Message Encryption:
    • Use AWS Key Management Service (KMS) to encrypt messages at rest.
  7. High Availability:
    • Fully managed service with built-in fault tolerance and scalability.
  8. Event-Driven Architecture:
    • Integrate with other AWS services for event-driven applications.

Components of SNS

  1. Topic:
    • A channel for messages to be published and subscribed to.
    • Types:
      • Standard Topic: High throughput, at-least-once delivery.
      • FIFO Topic: Ordered delivery, exactly-once processing.
  2. Subscriptions:
    • Represents the endpoint where messages are delivered.
    • Subscription protocols: HTTP/HTTPS, Lambda, SQS, Email, SMS, Mobile Push.
  3. Publisher:
    • The entity or application sending messages to an SNS topic.
  4. Subscriber:
    • The entity or application receiving messages from an SNS topic.

Use Cases of SNS

  1. Event Notification:
    • Notify multiple services or users about system or application events.
    • Example: Trigger notifications for EC2 instance state changes.
  2. Application Integration:
    • Decouple application components in microservices architectures.
  3. Monitoring and Alerts:
    • Send alerts for monitoring systems (e.g., CloudWatch alarm notifications).
  4. Mobile Messaging:
    • Deliver SMS or push notifications to mobile users.
  5. Broadcast Messaging:
    • Send announcements or updates to a wide audience via email or SMS.
  6. Workflow Orchestration:
    • Trigger workflows or downstream processes in response to specific events.

SNS Pricing

  1. Free Tier:
    • 1 million publishes and deliveries to HTTP/S, Lambda, or SQS.
    • 100 SMS deliveries in the free tier.
  2. Pay-As-You-Go:
    • Charges depend on the number of messages published and delivered, and the delivery protocol.
    • SMS messages are charged per message sent.
    • Email notifications are included in the free tier.
  3. Optional Features:
    • Costs for features like encryption (AWS KMS) and cross-region delivery.

Setting Up Amazon SNS

  1. Create a Topic:
    • Navigate to the SNS console → Topics → Create Topic.
    • Choose the topic type (Standard or FIFO).
    • Specify the topic name.
  2. Add Subscriptions:
    • In the SNS console, select a topic → Create Subscription.
    • Specify the protocol and endpoint (e.g., HTTP URL, email address, Lambda function ARN).
  3. Publish a Message:
    • Use the AWS SDK, CLI, or console to publish a message to the topic.

SNS Integration with Other AWS Services

  1. Amazon SQS:
    • Use SNS to fan out messages to multiple SQS queues.
  2. AWS Lambda:
    • Trigger serverless functions to process messages in real-time.
  3. Amazon CloudWatch:
    • Send alarms or metrics data to subscribers via SNS.
  4. Amazon EventBridge:
    • Forward events to SNS topics for further processing.

SNS with AWS SDK (Python Example)

import boto3

# Initialize SNS client
sns_client = boto3.client('sns', region_name='us-east-1')

# Create a topic
response = sns_client.create_topic(Name='MyTopic')
topic_arn = response['TopicArn']

# Subscribe to the topic
sns_client.subscribe(
    TopicArn=topic_arn,
    Protocol='email',  # Can also be 'sms', 'sqs', etc.
    Endpoint='example@example.com'
)

# Publish a message to the topic
sns_client.publish(
    TopicArn=topic_arn,
    Message='Hello from AWS SNS!',
    Subject='Test Notification'
)

Monitoring SNS

  • Use CloudWatch Metrics to monitor:
    • Number of messages published and delivered.
    • Message delivery success rates.
    • Throttling and errors.
  • Enable logging for troubleshooting.

Best Practices

  1. Use Dead-Letter Queues:
    • For debugging undelivered messages.
  2. Apply Message Filtering:
    • Minimize message overhead for subscribers.
  3. Secure Topics:
    • Use IAM policies to control access.
    • Encrypt messages with KMS.
  4. Optimize Delivery Protocols:
    • Use protocols suited to the use case (e.g., SQS for reliability, SMS for immediacy).
  5. Use FIFO for Ordered Processing:
    • Ensure strict order of message delivery when needed.

Alternatives to SNS

  • Amazon SQS:
    • Best for message queuing with guaranteed processing.
  • Amazon EventBridge:
    • Ideal for event-driven architectures with filtering and routing.
Back to top