The idea behind subscribing to SQS in SNS is to send messages from SNS to SQS. One can send both raw messages and any other message attributes included in the SNS message. Before proceeding with this article, it is assumed that you have an SNS and SQS in the account. The creation of SNS and SQS is not in the scope of this article.

SNS can have multiple SQS queues subscribed to it and SNS will publish the same message to all its subscriptions.

Go to AWS’s official page to know the pricing of SNS and SQS.

Pre-requisites

  1. AWS Account(Create if you don’t have one).
  2. SNS Topic (Click here to learn to create an SNS Topic)
  3. SQS Topic

What will we do?

  1. Login to AWS.
  2. Check SNS.
  3. Check SQS and Modify its access policy.
  4. Create a Subscription from SNS to SQS.
  5. Poll messages on SQS and send a message from SNS.
  6. Delete the subscription.

Login to AWS

To login into your AWS account click here and enter your credentials

How to create a subscription between an SQS queue and SNS topic on AWS linux

Once you successfully login into your account you will see the main AWS management console as follows.

How to create a subscription between an SQS queue and SNS topic on AWS linux

Check SNS

Search for SNS in the search box at the top of the screen.

How to create a subscription between an SQS queue and SNS topic on AWS linux

You will see the SNS dashboard as follows. Click on “Topics” in the left panel.

How to create a subscription between an SQS queue and SNS topic on AWS linux

Here, you will see the existing SNS topics in your account under the selected region. I will be using the “sns-test” SNS topic. Click on the desired topic to see its details.

How to create a subscription between an SQS queue and SNS topic on AWS linux

Here the topic does not have any subscription to it.

How to create a subscription between an SQS queue and SNS topic on AWS linux

Check SQS and Modify its access policy

Search for SQS in the search box at the top of the screen.

How to create a subscription between an SQS queue and SNS topic on AWS linux

You will see the main dashboard as follows for SQS with a list of SQS queues available under the selected region in your AWS account. Here, I will be using the “sqs-test” queue. Click on the SQS topic to check its details and modify the access policy.

How to create a subscription between an SQS queue and SNS topic on AWS linux

Go to the “Access policy” tab to modify the default/existing policy and click on the “Edit” button.

How to create a subscription between an SQS queue and SNS topic on AWS linux

In the access policy, replace the account arn with * under Principal block to avoid any potential access issues and save the changes. This is not recommended in the Production accounts.

How to create a subscription between an SQS queue and SNS topic on AWS linux

Create a Subscription from SNS to SQS

Go to the SNS topic and click on the “Create subscription” button to create a subscription.

How to create a subscription between an SQS queue and SNS topic on AWS linux

Here, change the protocol to Amazon SQS and specify the ARN of the SQS topic in the endpoint field. Check the “Enable raw message delivery” checkbox and then click on the “Create subscription” button.

How to create a subscription between an SQS queue and SNS topic on AWS linux

Now, if you go to the SNS topic you will see that the subscription has successfully been created and the status is confirmed.

How to create a subscription between an SQS queue and SNS topic on AWS linux

Poll messages on SQS and send a message from SNS.

We can test if messages are arriving in SQS when sent from the SNS topic. To test this, go to the SQS topic and click on the “Send and receive messages” button.

How to create a subscription between an SQS queue and SNS topic on AWS linux

Here click on the “Pool for messages” button.

How to create a subscription between an SQS queue and SNS topic on AWS linux

As soon as you click on the “Poll for messages” button, go to the SNS topic and click on the “Publish message” button.

How to create a subscription between an SQS queue and SNS topic on AWS linux Advertisement

Here, add a subject to the message and select a “Custom payload for each delivery protocol” option.

How to create a subscription between an SQS queue and SNS topic on AWS linux

Scroll down a bit and click on the “Publish message” button.

How to create a subscription between an SQS queue and SNS topic on AWS linux

Now, if you come back to the SQS page, you will see that the message has been received by SQS.

Click on the message ID to see what has been sent from SNS.

How to create a subscription between an SQS queue and SNS topic on AWS linux

In the body section, you can see that the message which was sent by SNS has been successfully received by the SQS.

How to create a subscription between an SQS queue and SNS topic on AWS linux

Delete the subscription

If you no longer need the subscription you can delete it easily from the SNS topic itself. Select the subscription and click on the “Delete” button. 

How to create a subscription between an SQS queue and SNS topic on AWS linux

You will get a pop-up screen to confirm your deletion. Once the subscription is deleted you cannot restore it. Be careful while performing deletion operations in your production accounts.

How to create a subscription between an SQS queue and SNS topic on AWS linux

Conclusion

In this article, we saw the steps to create a subscription from the SNS topic to the SQS topic. We sent a sample message from SNS which was polled in the SQS. We also saw how easily a subscription can be deleted from the SNS.