LocalStack SNS+SQS

In this article, We will show you how you can use LocalStack to test AWS SNS (Simple Notification Service) and SQS (Simple Queue Service) on your local system.

Localstack SNS+SQS

Video

Localstack SQS+SNS

What is LocalStack?

LocalStack provides an easy-to-use test/mocking framework for developing Cloud applications. This means you can test AWS cloud resources locally on your machine.

Note: LocalStack supporting only AWS cloud stack.

LocalStack spins up the following core Cloud APIs on your local machine.

  • ACM, API Gateway, CloudFormation, CloudWatch
  • CloudWatch Logs, DynamoDB, DynamoDB Streams
  • EC2, Elasticsearch Service, EventBridge (CloudWatch Events)
  • Firehose, IAM, Kinesis, KMS, Lambda, Redshift
  • Route53, S3, SecretsManager, SES, SNS
  • SQS, SSM, StepFunctions, STS

Benefits of Using LocalStack

  • Reduce Cost
  • Test AWS Cloud Resource Locally
  • Learn AWS Cloud Resource Locally
  • Debug Locally

Let’s Start

We hope you have installed Docker on your system else you can check the following article to install Docker.

Make Sure, You have configured AWS CLI on your system. If not please read the following article.

Docker-Compose.YML

Note: Starting with version 0.11.0, all APIs are exposed via a single edge service, which is accessible on http://localhost:4566 by default. If you look into the following file we have used EDGE_PORT=4566.

In the above file, We are creating Two Services as below with default region us-east-1

  1. SQS
  2. SNS

If you run the following command

docker-compose up

You will see the LocalStack container is running successfully as shown below in the screenshot

How To Test Services

To check all the services type the following URL on your browser

http://localhost:4566/health

So now, the SNS and SQS services are running on the local system with the help of LocalStack

Create SQS Queue Locally

Once your LocalStack container is running up. You can open a new terminal and create an SQS queue using the following command

aws --endpoint-url=http://localhost:4566 sqs create-queue --queue-name onexlab

The above command will create the SQS queue name onexlab in the LocalStack SQS service. You can see the SQS queue URL in the screenshot shown below

List SQS Queue

To check all the created queues in the LocalStack type the following command in the terminal

aws --endpoint-url=http://localhost:4566 sqs list-queues

Read SQS Queue Messages

To read the SQS Queue Messages type the following command in the terminal

aws --endpoint-url=http://localhost:4566 sqs receive-message --queue-url http://localhost:4566/000000000000/onexlab

If you look at the following screenshot you can see there not message in the SQS queue. It’s empty, we will push the message using the SNS topic

Create SNS Topic

To create the SNS topic type the following command

aws --endpoint-url=http://localhost:4566 sns create-topic --name onexlab-sns

The above command will create the SNS topic name onexlab-sns as shown in the following screenshot

List SNS Subscription

To check the subscription of the SNS service type the following command

aws --endpoint-url=http://localhost:4566 sns list-subscriptions

The above command will show the list of subscriptions as shown below

If you look at the above screenshot Subscriptions[] array is empty because we haven’t subscribed yet.

Subscribe to SNS Topic using SQS

To subscribe to the SNS topic we need the following endpoints

  1. SNS Topic (arn:aws:sns:us-east-1:000000000000:onexlab-sns)
  2. SQS Queue Name (http://localhost:4566/000000000000/onexlab)

Use the following command to subscribe.

aws --endpoint-url=http://localhost:4566 sns subscribe --topic-arn arn:aws:sns:us-east-1:000000000000:onexlab-sns --protocol sqs --notification-endpoint http://localhost:4566/000000000000/onexlab

You will see we have subscribed to the SNS topic as shown in the following screenshot

SNS Publish Message To Topic

We have already created the SNS topic let’s push the message to the SNS Topic. To publish the message to SNS Topic use the following command

aws --endpoint-url=http://localhost:4566 sns publish  --topic-arn arn:aws:sns:us-east-1:000000000000:onexlab-sns --message 'Welcome to Onexlab!'

In the above screenshot, we have pushed the message to the SNS topic ‘Welcome to Onexlab!’

Let’s Read the message in SQS Queue

We have already pushed the message to the SNS topic. To check the message in the SQS queue run the following command

aws --endpoint-url=http://localhost:4566 sqs receive-message --queue-url http://localhost:4566/000000000000/onexlab

In the above screenshot, you can see the SQS queue we have the message that we published to the SNS topic. Also, verify the MessageID it’s from the SNS topic.

Thank you 🚀🚀🚀🚀🚀🚀

Teaching & Developing For Startups

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store