Sunday, July 20, 2025

Amazon Simple Notification Service(SNS).

Amazon Simple Notification Service(SNS)

Amazon SNS is a fully managed pub/sub (publish/subscribe) messaging service used to send messages from producers (publishers) to multiple consumers (subscribers) asynchronously.

Key Use Cases

Use Case

Description

Application decoupling

Producers publish messages; subscribers process independently.

Fan-out pattern

One message sent to many endpoints (e.g., Lambda, SQS, HTTP).

Mobile push notifications

Sends notifications to iOS, Android, Fire OS devices.

Email/SMS alerts

Sends alerts to users or admins.

Workflow triggers

Event-based architecture (e.g., image upload → start processing pipeline).

 Core Concepts

  • Topic: A logical access point where publishers send messages.
  • Publisher: The sender (e.g., application, Lambda, script).
  • Subscriber: Endpoints that receive messages from a topic.
  • Subscription: A configuration that connects a topic to a subscriber.

 Supported Protocols for Subscribers

Protocol

Example

SQS

Queue receives message. Good for decoupling or buffering.

Lambda

Message triggers Lambda function.

HTTP/S

POSTs message to a webhook or REST endpoint.

Email

Sends text message to an email address.

SMS

Sends text message to a mobile phone.

Application

Sends push notifications to mobile devices.

 How SNS Works (Fan-out Example)

# Plaintext

      [Publisher]

           |

           V

      [SNS Topic] twtech-SNS-Topic

           |                        |

           |                        |

       [SQS Queue]            [Lambda Function]

  • One message sent to the SNS topic.
  • All subscribers receive a copy of the message simultaneously.

 Security & Permissions

  • IAM policies: Control who can publish or subscribe.
  • Topic access policies: Restrict which AWS accounts/services can interact.
  • Encryption: Support for server-side encryption using AWS KMS.
  • Message filtering: Subscribers can filter messages by attributes (e.g., only receive certain event types).

 Features at a Glance

Feature

Description

Message fan-out

Send a message to many endpoints.

Message filtering

Subscribers get only messages they care about.

Durability

High availability and redundancy.

Push-based

Instantly delivers messages — no polling.

Global reach

SMS/email/mobile push globally.

 SNS vs. SQS

Feature

SNS

SQS

Model

Pub/Sub (Push)

Message Queue (Pull)

Delivery

Pushes to multiple subscribers

Consumers poll for messages

Use Case

Notifications, fan-out, alerts

Decoupling, buffering, retries

Ordering

No ordering guarantees

FIFO queues available

Retry Logic

Retry per subscriber protocol

Built-in redrive policy

 Example: Using SNS + SQS Fan-Out

  1. Create an SNS topic.
  2. Create two SQS queues.
  3. Subscribe both SQS queues to the SNS topic.
  4. Publish a message to the topic → both queues receive a copy.

twtech insights:

What "Asynchronously" Means in AWS Context.

"Asynchronously" means that tasks or communication between services happen independently and non-blocking — the sender doesn’t wait for the receiver to finish processing.

Example in AWS: SNS + SQS = Asynchronous Flow

Let’s say you have a web application that processes image uploads.

Without Asynchronous Messaging (synchronous):

# plaintext
[Web App]
    |
    | (publishes)
    V
  [SNS Topic]
    |
    | (forwards message)
    V
  [SQS Queue] ---> [Worker Processes] ---> [Database]

·        The web app publishes a message to SNS.

·        SNS asynchronously delivers the message to an SQS queue.

·        Worker processes pull from the queue and process the image independently.

·        The web app can immediately return a response like “Upload received.”

 Benefits of Asynchronous Design

Benefit

Why It Matters

Low latency

Sender doesn't wait — better user experience.

Resilience

Systems continue operating even if downstream is slow/down.

Scalability

Easier to scale consumers based on message volume.

Decoupling

Components evolve independently — no direct dependencies.

Retry logic

Messages can be retried without re-sending from sender.

  Asynchronous Tools in AWS

Service

Role in Asynchronous Communication

Amazon SQS

Buffer messages for consumers to process later.

Amazon SNS

Publish messages to many consumers instantly.

AWS Lambda

Responds to events without needing direct integration.

EventBridge

Event bus for routing events across AWS services.

How twtech may send a single massages to may recievers.

NB:

twtech may have up to 12,500,000 subscriptions per topic.

 twtech may have up to 100,000 topics.

SNS integrates with a lot of AWS services. The following services can send data directly to SNS for notifications.

·        CloudWatch Alarms,

·        S3 Bucket (Events),

·         Auto Scaling Group (Notifications),

·        CloudFormation (State Changes),

·        AWS Budgets,

·        Lambda,

·         AWS DMS (New Replic),

·        DynamoDB RDS Events.

Project: Hands-on

How twtech creates and use SNS for Messagig.

Search for the aws service: SNS (Simple Notification Service)


Add a topic name and create an SNS topic: twtech-SNS-Topic

Select Type: Standard

Topic type cannot be modified after topic is created.

Encryption - optional

Amazon SNS provides in-transit encryption by default. Enabling server-side encryption adds at-rest encryption to your topic.


Access policy optional

This policy defines who can access your topic. By default, only the topic owner can publish or subscribe to the topic.

Data protection policy optional

This policy defines which sensitive data to monitor and to prevent from being exchanged via your topic.

Delivery policy (HTTP/S) - optional

The policy defines how Amazon SNS retries failed deliveries to HTTP/S endpoints. To modify the default settings, expand this section.

Delivery status logging optional

These settings configure the logging of message delivery status to CloudWatch Logs.

Tags optional

A tag is a metadata label that you can assign to an Amazon SNS topic. Each tag consists of a key and an optional value. You can use tags to search and filter your topics and track your costs. 


Active tracing optional

Use AWS X-Ray active tracing for this topic to view its traces and service map in Amazon CloudWatch. Additional costs apply.

Create sns topic: twtech-SNS-Topic

How twtech creates its subscriptions:

To Create subscription

Remember to select the appropriate protocol(if pattern used is): Email

 And

Confirm subscription after creating from email used: twtech671@gmail.com 

Subscription filter policy optional

This policy filters the messages that a subscriber receives.

Redrive policy (dead-letter queue) - optional

Send undeliverable messages to a dead-letter queue.

Redrive policy (dead-letter queue)


Create subscription:

From:

To:

Confirm subscription on the used email: twtech671@gmail.com

By clicking on the link: twtech Confirms subscription.



Go back to SNS topic created and refresh page: To see status

From:

To: refresh page.

How twtech publishes messages on the  SNS Topic: twtech-SNS-Topic


Message details

Message body

Message attributes

Message attributes let you provide structured metadata items (such as timestamps, geospatial data, signatures, and identifiers) for the message.


Publish the message:

Check email used in subscription for the published message: twtech671@gmail.com

Verify content of message form SNS created

How twtech creates a subscription with another pattern (protocol): Amazon SQS pattern

Select pattern of choice (protocol): Amazon SQS pattern

Setup endpoint: Only Amazon SQS standard queues will be listed and can receive notifications from an Amazon SNS standard topic.


No comments:

Post a Comment

Kubernetes Clusters | Upstream Vs Downstream.

  The terms "upstream" and "downstream" in the context of Kubernetes clusters often refer to the direction of code fl...