Create an SQS Queue and an SNS Topic and Subscription using the AWS CLI

AWS gives engineers many ways to accomplish many tasks. Here we will look at creating both a Simple Notification Service topic as well as a Simple Queue Service queue and then creating an SNS subscription “linking” the two. All the while, we will exclusively use the AWS CLI to accomplish the task. This exercise could also be carried out in  the AWS console as well as one of the AWS SDKs and if you are proficient in an AWS SDK or the console, feel free to use one of those tools to view the changes being made while we move through this solution.

The Solution

In this tutorial, we’ll take you through setting up an Amazon Simple Notification Service topic, as well as a Simple Queue Service queue and then subscribe the SQS queue to the SNS topic. Note, we will interchangeably use SNS to refer to Simple Notification Service and SQS to refer to Simple Queue Service as a way to become familiar with the AWS vernacular.

Remember, for any example solution from AWS with .NET, we focus on the code that exemplifies the problem we are trying to solve. We don’t include logging, input validation, exception handling, etc., and we embed the configuration data within classes instead of using environment variables, configuration files, key/value stores and the like. These items should not be skipped for proper solutions.


To complete this solution, you will need to download the AWS CLI and configure your environment for the CLI. In addition, you will need to create an AWS IAM user with programmatic access with the appropriate permissions for Amazon SNS and Amazon SQS.

Warning: some AWS services may have fees associated with them.

Creating the Amazon SNS Topic

Now that the AWS CLI is downloaded and your environment configured, we need to run the following command at the CLI to create our SNS topic.

$ aws sns create-topic ––name pub-sub-test

If successful, the command will return with a response like the following. Record the TopicArn value for later configuration.

“TopicArn”: “arn:aws:sns:us-east-1:<aws-account-number>:pub-sub-test”

Creating the Amazon SQS Queue

For this exercise, we will use an Amazon SQS queue to consume the messages from the SNS topic. However, in order for Amazon SNS to route a message to the Amazon SQS queue we create, we need to set up the appropriate access policy. The way this is done using the AWS CLI is to put the policy document within another JSON structure, in a separate file. We’ll call that file sqs-attributes.json. You can see what the file looks like below and you can download it here. Note: remember to replace the <aws-account-number> place holder with your AWS account number.

{“Policy”:”{“Version”: “2012-10-17″,”Id”: “arn:aws:sqs:us-east-1:<aws-account-number>:undefined/SQSDefaultPolicy”,”Statement”:[{“Sid”: “topic-subscription-arn:aws:sns:us-east-1:<aws-account-number>:pub-sub-test”,”Effect”: “Allow”,”Principal”: {“Service”: “”},”Action”: “SQS:SendMessage”,”Resource”: “arn:aws:sqs:us-east-1:<aws-account-number>:pub-sub-test-consumer”,”Condition”: {“ArnEquals”: {“aws:SourceArn”: “arn:aws:sns:us-east-1:<aws-account-number>:pub-sub-test”}}}]}”}

Notice, the bolded text is the actual policy document.

With that file in place we are now ready to create the SQS queue, referencing the file we just created.

$ aws sqs create-queue ––queue-name pub-sub-test-consumer ––attributes file://sqs-attributes.json

If successful, the command will return with a response like the following. Record the QueueUrl value for later configuration.

“QueueUrl”: “<aws-account-number>/pub-sub-test-consumer&#8221;

Creating the Amazon SNS Subscription

With the Amazon SNS topic and Amazon SQS queue in place, we now need to configure Simple Notification Service to route messages from the SNS topic, to the SQS queue, by creating an SNS subscription. First, we need to obtain the ARN of the queue that we just created, using the previously recorded QueueUrl. In order to do so, we use the following command.

$ aws sqs get-queue-attributes ––queue-url<aws-account-number>/pub-sub-test-consumer ––attribute-names QueueArn

If successful, the command will return with a response like the following. Record the QueueArn value for later configuration.

“Attributes”: {
“QueueArn”: “arn:aws:sqs:us-east-1:<aws-account-number>:pub-sub-test-consumer”

Now we are ready to create the SNS subscription via the CLI. Here we specify the “sqs” protocol and we’ll also set the –topic-arn value to the TopicArn value we recorded earlier as well as set the –notification-endpoint to the previously recorded SQS QueueArn. In addition, we will set the queue to deliver a raw message instead of a message with the SNS wrapper. Putting it all together looks like this:

$ aws sns subscribe ––topic-arn arn:aws:sns:us-east-1:<aws-account-number>:pub-sub-test ––protocol sqs ––notification-endpoint arn:aws:sqs:us-east-1:<aws-account-number>:pub-sub-test-consumer ––attributes RawMessageDelivery=true

If successful, the command will return with a response like the following and you’ll be ready to send messages to SNS and receive them from SQS.

“SubscriptionArn”: “arn:aws:sns:us-east-1:<aws-account-number>:pub-sub-test:3ed16e7b-a890-4f89-b0c7-b51ejy034d33”

To test the publication of a message to the SNS topic, use the following command.

$ aws sns publish ––topic-arn arn:aws:sns:us-east-1:<aws-account-number>:pub-sub-test ––message “test message for pub-sub-test topic”

On Success, a response will appear.

“MessageId”: “6l906d6c-f0fa-524l-bce1-34e7b0f3a28a”

Now, lets go on the other side of the subscription to read that message using the SQS CLI client.

$ aws sqs receive-message ––queue-url<aws-account-number>/pub-sub-test-consumer

A successful response will look something like the following. Check out the value of the body and you’ll see the message that was sent.

“Messages”: [
“MessageId”: “46d9c87d-1cc9-4dc9-b838-366246d5d”,
“ReceiptHandle”: “AQEBtp8hMcIZuUqgtIfrtxg7YDcRSQmdPdZ4rCRBvGvlOJvKuRm7U2Yzl7DtX2Svth5K315QGx1Bxvug6AEk3Bd+uQNXGJLyAfFo1ZE9l0cb+Kgwe41ApCARySNg+W2zDQeCCYbt7fVy0JA9aJUbiS7o+8mK3MbhmKCT3mwTgO+LF/vPWw2uSp9fNW+LzL0Y5jjS39aezmjBDzyP+TalWa7fhZ6kzMtc8oHx1+HiP2GElZ1tW8YkDNutHyGk0C/U30SG7bA2D5VRF/J/N6F6eExrw+F0bPWZ6HHmo8ZF7fiYNY6dOvIEr7cp3EwttC1voJK2JTuN/bCdWU5BIRZTz27aS1NURQ==”,
“MD5OfBody”: “9ofd4dd7d776a5c9”,
“Body”: “test message for pub-sub-test topic”

There you have it, your Amazon SNS and Amazon SQS infrastructure is set up. Stay tuned for a future tutorial where we will build a .NET message publisher and we’ll utilize these same AWS CLI techniques.