# AWS SNS

## What is SNS?

* SNS stands for Simple Notification Service.
* It is a web service which makes it easy to set up, operate, and send a notification from the cloud.
* It provides developers with the highly scalable, cost-effective, and flexible capability to publish messages from an application and sends them to other applications.
* It is a way of sending messages. When you are using AutoScaling, it triggers an SNS service which will email you that "your EC2 instance is growing".
* SNS can also send the messages to devices by sending push notifications to Apple, Google, Fire OS, and Windows devices, as well as Android devices in China with Baidu Cloud Push.
* Besides sending the push notifications to the mobile devices, Amazon SNS sends the notifications through SMS or email to an Amazon Simple Queue Service (SQS), or to an HTTP endpoint.
* SNS notifications can also trigger the Lambda function. When a message is published to an SNS topic that has a Lambda function associated with it, Lambda function is invoked with the payload of the message. Therefore, we can say that the Lambda function is invoked with a message payload as an input parameter and manipulate the information in the message and then sends the message to other SNS topics or other AWS services.
* Amazon SNS allows you to group multiple recipients using topics where the topic is a logical access point that sends the identical copies of the same message to the subscribe recipients.
* Amazon SNS supports multiple endpoint types. For example, you can group together IOS, Android and SMS recipients. Once you publish the message to the topic, SNS delivers the formatted copies of your message to the subscribers.
* To prevent the loss of data, all messages published to SNS are stored redundantly across multiple availability zones.

### SNS Publishers and Subscribers

![SNS](https://static.javatpoint.com/tutorial/aws/images/sns-clients.png)

Amazon SNS is a web service that manages sending messages to the subscribing endpoint. There are two clients of SNS:

* Subscribers
* Publishers

<figure><img src="https://static.javatpoint.com/tutorial/aws/images/aws-sns.png" alt=""><figcaption></figcaption></figure>

**Publishers**

Publishers are also known as producers that produce and send the message to the SNS which is a logical access point.

**Subscribers**

Subscribers such as web servers, email addresses, Amazon SQS queues, AWS Lambda functions receive the message or notification from the SNS over one of the supported protocols (Amazon SQS, email, Lambda, HTTP, SMS).

**Note: A publisher sends the message to the SNS topic that they have created. There is no need to specify the destination address while publishing the message as the topic itself matches the subscribers associated with the topic that the publisher has created and delivers the message to the subscribers.**

### How to use SNS

* Move to the SNS service available under the application services.

<figure><img src="https://static.javatpoint.com/tutorial/aws/images/aws-sns2.png" alt=""><figcaption></figcaption></figure>

* Click on the **Topics** appearing on the left side of the Console.

<figure><img src="https://static.javatpoint.com/tutorial/aws/images/aws-sns3.png" alt=""><figcaption></figcaption></figure>

* Click on the **Create Topic** to create a new topic.

<figure><img src="https://static.javatpoint.com/tutorial/aws/images/aws-sns4.png" alt=""><figcaption></figcaption></figure>

* Enter the topic name in a text box.

<br>

<figure><img src="https://static.javatpoint.com/tutorial/aws/images/aws-sns5.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://static.javatpoint.com/tutorial/aws/images/aws-sns6.png" alt=""><figcaption></figcaption></figure>

* The below screen shows that the topic has been created successfully.

<figure><img src="https://static.javatpoint.com/tutorial/aws/images/aws-sns7.png" alt=""><figcaption></figcaption></figure>

* To create a subscription, click on the **Create subscription**.

<figure><img src="https://static.javatpoint.com/tutorial/aws/images/aws-sns8.png" alt=""><figcaption></figcaption></figure>

* Now, choose the endpoint type and enter the Endpoint address, i.e., where you want to send your notification.

<figure><img src="https://static.javatpoint.com/tutorial/aws/images/aws-sns9.png" alt=""><figcaption></figcaption></figure>

* The below screen shows that the status of subscription is pending.

<figure><img src="https://static.javatpoint.com/tutorial/aws/images/aws-sns10.png" alt=""><figcaption></figcaption></figure>

* The below screen shows that mail has been sent to the subscriber. A Subscriber has to click on the **Confirm Subscription**.

<br>

<figure><img src="https://static.javatpoint.com/tutorial/aws/images/aws-sns11.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://static.javatpoint.com/tutorial/aws/images/aws-sns12.png" alt=""><figcaption></figcaption></figure>

* Click on the topic name, i.e., hello and then click on the **Publish message**.

<figure><img src="https://static.javatpoint.com/tutorial/aws/images/aws-sns13.png" alt=""><figcaption></figcaption></figure>

* Enter the subject, Time to Live and Message body to send to the endpoint.

\
![SNS](https://static.javatpoint.com/tutorial/aws/images/aws-sns15.png)

<figure><img src="https://static.javatpoint.com/tutorial/aws/images/aws-sns14.png" alt=""><figcaption></figcaption></figure>

* The message has been sent to all the subscribers that have been mentioned in the ID.

<figure><img src="https://static.javatpoint.com/tutorial/aws/images/aws-sns16.png" alt=""><figcaption></figcaption></figure>

### Benefits of SNS

<figure><img src="https://static.javatpoint.com/tutorial/aws/images/benefits-of-sns.png" alt=""><figcaption></figcaption></figure>

* **Instantaneous delivery**\
  SNS is based on push-based delivery. This is the key difference between SNS and SQS. SNS is pushed once you publish the message in a topic and the message is delivered to multiple subscribers.
* **Flexible**\
  SNS supports multiple endpoint types. Multiple endpoint types can receive the message over multiple transport protocols such as email, SMS, Lambda, Amazon SQS, HTTP, etc.
* **Inexpensive**\
  SNS service is quite inexpensive as it is based on pay-as-you-go model, i.e., you need to pay only when you are using the resources with no up-front costs.
* **Ease of use**\
  SNS service is very simple to use as Web-based AWS Management Console offers the simplicity of the point-and-click interface.
* **Simple Architecture**\
  SNS is used to simplify the messaging architecture by offloading the message filtering logic from the subscribers and message routing logic from the publishers. Instead of receiving all the messages from the topic, SNS sends the message to subscriber-only of their interest.

### Differences b/w SNS and SQS

* SNS stands for **Simple Notification Service** while SQS stands for **Simple Queue Service**.
* SQS is a pull-based delivery, i.e., messages are not pushed to the receivers. Users have to pull the messages from the Queue. SNS is a push-based delivery, i.e., messages are pushed to multiple subscribers.
* In SNS service, messages are pushed to the multiple receivers at the same time while in SQS service, messages are not received by the multiple receivers at the same time.
* SQS polling introduces some latency in message delivery while SQS pushing pushed the messages to the subscribers immediately.

<br>
