AWS SNS and SQS

john sunam
3 min readMar 21, 2021

In this blog, I am going to talk about two AWS services(SNS and SQS), how we can use them together, and show you how they work with each other.

Amazon Simple Notification Service

Amazon Simple Notification Service (Amazon SNS) is a fully managed messaging service for both application-to-application (A2A) and application-to-person (A2P) communication.

Amazon SNS service consists of many two import parts one is Topics and the other is Subscriptions. SNS actually works on PubSub Model. PubSub model actually simplifies the understanding of how SNS service works. According to this model, there are always two parties one is Publisher and the other is Subscriber.

Fig-1.0

Publisher: The publisher is the one who sends payload messages to the AWS SNS service. SNS service fanout this publisher message to different consumers who have subscribed to that specific SNS topic. The publisher can be any AWS service or any other application.

Subscriber: The subscriber is the one who subscribes to the SNS topic. So when the publisher publishes some message to the SNS topic, the topic will fanout this message to all the subscribers of that SNS topic. In the above figure 1.0, you can see that subscribers can be different AWS services, endpoints, and so on.

AWS Simple Message Queue Servie

AWS SQS service is a fully managed message queuing service. Using SQS, we can send, store, and receive messages between software components at any volume, without losing messages or requiring other services to be always available.

Fig-1.1

The one who sends the message to the SQS queue is a producer and the one who consumes that message from the SQS queue is a consumer.

When the producer sends messages to the SQS queue consumer will be notified that there is a message in the queue. The consumer will poll that message from the queue, processes it, and delete the message from the queue. This is basically a normal flow about how SQS works internally.

I think till now we already have how these two services basically work. In a most real-world scenario, we will come across the situation where we had to use these two services in a combined way. I will show you how we can subscribe to the current SNS service from the SQS queue. So that when any publisher publishes a message to SNS topic then that will fanout message to subscribing SQS queue.

SNS topic template notification.yaml

AWSTemplateFormatVersion: '2010-09-09'
Description: SNS Topic
Resources:
SNSTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: Test sns topic
Outputs:
TopicARN:
Description: SNS topic for test
Value: !Ref SNSTopic
Export: TestSNSTopic

In the above template, we have created a new SNS topic and exported SNS ARN with the name `TestSNSTopic` which we will use later while subscribing to this topic from the SQS queue.

SQS queue template which is subscribing above SNS Topic queue.yaml

AWSTemplateFormatVersion: '2010-09-09'
Description: Test Queue
Resources:
TestQueueDLQ:
Type: AWS::SQS::Queue
Properties:
VisibilityTimeout: 300

TestQueue:
Type: AWS::SQS::Queue
Properties:
VisibilityTimeout: 300
QueueName: test-queue
RedrivePolicy:
deadLetterTargetArn: !GetAtt TestQueueDLQ.Arn
QueuePolicy:
DependsOn: TestQueue
Type: AWS::SQS::QueuePolicy
Properties:
Queues:
- !Ref TestQueue
PolicyDocument:
Statement:
- Resource: !GetAtt TestQueue.Arn
Effect: Allow
Sid: "Allow-SNS-SQS-Message"
Principal: "*"
Action:
- "sqs:SendMessage"
- "sqs:ReceiveMessage"
QueueSubscription:
Type: AWS::SNS::Subscription
Properties:
Protocol: sqs
Endpoint: !GetAtt TestQueue.Arn
TopicArn: TestSNSTopic

You can see in the above SQS queue template that I have added QueueSubscription block which will subscribe to the SNS topic TestSNSTopic that we have created in the SNS template. By creating both of the stacks above we will be able to integrate SNS and SQS service. When you publish any message on the SNS topic then it will broadcast to the above TestQueueas well. After that consumer of the above queue will poll the message as process it.
Hope this blog will be helpful for you to understand and get the basic knowledge about how SQS and SNS work with each other.

--

--

No responses yet