Setting up Localstack and creating SQS Queues and SNS Topics

11:38 AM , , , 0 Comments


What is Localstack?
Localstack is a fully functional local AWS cloud stack which enables you to develop and test your cloud apps offline! In this post I will teach you how to install it, and how to create SQS Queues and SNS Topics that reside on localstack.

How do we install Localstack?
We can install localstack by running this command on our terminal:
pip install localstack

If you don't want to install the prerequisites needed to run localstack, we can use docker to pull localstack without manually installing the dependencies ourselves. This will save us a lot of time and hair! Just pull the localstack repository from github!
git clone https://github.com/localstack/localstack.git

How do we run Localstack?
To run localstack, just enter this command on the terminal:
localstack start
If you went with docker, we can use this command:
cd path/to/localstack
docker-compose up
Wait for the word "ready", that's when you know that it's up and running.
Starting localstack_localstack_1 ... 
Starting localstack_localstack_1 ... done
Attaching to localstack_localstack_1
localstack_1  | 2018-11-16 08:35:13,572 CRIT Supervisor running as root (no user in config file)
localstack_1  | 2018-11-16 08:35:13,597 INFO supervisord started with pid 1
localstack_1  | 2018-11-16 08:35:14,600 INFO spawned: 'dashboard' with pid 11
localstack_1  | 2018-11-16 08:35:14,602 INFO spawned: 'infra' with pid 12
localstack_1  | (. .venv/bin/activate; bin/localstack web)
localstack_1  | (. .venv/bin/activate; exec bin/localstack start)
localstack_1  | 2018-11-16 08:35:15,632 INFO success: dashboard entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
localstack_1  | 2018-11-16 08:35:15,633 INFO success: infra entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
localstack_1  | Starting local dev environment. CTRL-C to quit.
localstack_1  | 2018-11-16T08:35:21:INFO:werkzeug:  * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
localstack_1  | 2018-11-16T08:35:21:INFO:werkzeug:  * Restarting with stat
localstack_1  | 2018-11-16T08:35:22:WARNING:werkzeug:  * Debugger is active!
localstack_1  | 2018-11-16T08:35:22:INFO:werkzeug:  * Debugger PIN: 228-822-518
localstack_1  | Starting mock API Gateway (http port 4567)...
localstack_1  | Starting mock DynamoDB (http port 4569)...
localstack_1  | Starting mock SES (http port 4579)...
localstack_1  | Starting mock Kinesis (http port 4568)...
localstack_1  | Starting mock Redshift (http port 4577)...
localstack_1  | Starting mock S3 (http port 4572)...
localstack_1  | Starting mock CloudWatch (http port 4582)...
localstack_1  | Starting mock CloudFormation (http port 4581)...
localstack_1  | Starting mock SSM (http port 4583)...
localstack_1  | Starting mock SQS (http port 4576)...
localstack_1  | Starting mock Secrets Manager (http port 4584)...
localstack_1  | Starting local Elasticsearch (http port 4571)...
localstack_1  | Starting mock SNS (http port 4575)...
localstack_1  | Starting mock DynamoDB Streams service (http port 4570)...
localstack_1  | Starting mock Firehose service (http port 4573)...
localstack_1  | Starting mock Route53 (http port 4580)...
localstack_1  | Starting mock ES service (http port 4578)...
localstack_1  | Starting mock Lambda service (http port 4574)...
localstack_1  | Ready.

How do we run Localstack on a different port?
As you will see above, when localstack is running it will try to use port 8080. It's a pretty common port for most apps, and we might not want to run it on that port. So how do we run it with a different port you ask? Well, let's say you want to run it on port 9182, just use this command:
PORT_WEB_UI=9182 docker-compose up

How do we create aws resources with localstack?
Creating an aws resource with localstack is very easy. All you have to do is refer to AWS CLI documentation and use the --endpoint-url option. So in this post, we'll try to create an SQS queue and subscribe to a topic. Take note, that the resources we'll be creating will not be created on AWS, you will not see them on your AWS console/website, it will just reside on your local machine. Also, whenever you close localstack, the resources will get deleted, so you'll have to recreate them again each run.

Create SQS queue
Below is the command to create an SQS queue. Notice how we used http://localhost:4576 as the endpoint-url? If you don't know what port to use, you can refer to the logs that localstack outputs each time it runs. It lists the service and ports there. Isn't it great?
aws --endpoint-url=http://localhost:4576 sqs create-queue --queue-name my-queue-001

Create SNS Topic
To create an SNS topic, we'll have to use the command below. Similar to the command above, we used http://localhost:4575 to indicate that the resource should be created in localstack.
aws --endpoint-url=http://localhost:4575 sns create-topic --name my-topic-name

Subscribing a queue to our topic
Finally, to subscribe a queue to our topic, we'll have to use this command:
aws --endpoint-url=http://localhost:4575 sns subscribe --topic-arn arn:aws:sns:us-east-1:123456789012:my-topic-name --protocol sqs --notification-endpoint http://localhost:4576/queue/my-queue-001

As you can see, using localstack is very convenient and easy, what's even better is you can save on cost. Instead of testing stuff on your actual aws account, you can test it locally with localstack.

Meep Musings

I am a free lamb who loves travels around the world, exploring different cultures, meeting interesting people and I talking about anything and everything under the sun. :)

No comments :