For this purpose we use the public localstack
docker image. To see more documentation please see the following repository: https://github.com/localstack/localstack
First set a new profile for aws on your system.
Set test as aws_access_key_id
and aws_secret_access_key
.
Set us-east-1 for the region
in the profile configuration.
aws configure --profile localstack
By default on the docker-compose.yml
for the localstack
service
Under volume we run always a setup script tha can be found in the following path /scripts/localstack-entrypoint
This script configures localstack we an exact replica of the cloud configuration.
For the most cases we have one normal queue and one dead letter queue for each microservice.
- demo-[env]-sales
- demo-[env]-inventory
- demo-[env]-notifications
*[env] can be replaced by local
(default) on local setup(see /scripts/localstack-entrypoint
), development
, staging
, production
There is a topic for each microservice
- demo-[env]-sales
- demo-[env]-inventory
- demo-[env]-notifications
This queue listens to the following topics
- notifications
- inventory
This queue listens to the following topics
- sales
- notifications
This queue listens to the following topics
- sales
- inventory
To build the image and have it ready run the following command in the current workspace.
docker-compose up localstack
When you see the following message Localstack is ready. This means localstack has been successfully setup.
Before you can start the other microservices we need to change some environment variables please make sure all your .env
files under packages have the following configuration
AWS_SNS_TOPIC_ARN_PREFIX=arn:aws:sns:us-east-1:000000000000
AWS_SQS_QUEUE_DOMAIN=http://localstack:4566/000000000000
AWS_ACCESS_KEY=test
AWS_SECRET_KEY=test
AWS_REGION=us-east-1
LOCALSTACK_ENDPOINT=http://localstack:4566
*Note1:You can interchange the variable localstack
for host.docker.internal
on the urls. (Windows only fix)
To start the other microservices run the following command
docker-compose up sales inventory notifications
To check the setup is done we can try the following commands
1)To list all sqs queues
aws --endpoint-url=http://localhost:4566 sqs list-queues --profile localstack
2)To list all topics
aws --endpoint-url=http://localhost:4566 sns list-subscriptions --profile localstack
3)To purge all queues (deletes all messages from quues)
aws sqs purge-queue --endpoint-url=http://localhost:4566 --queue-url http://localhost:4566/000000000000/annise-local-sales --profile localstack
aws sqs purge-queue --endpoint-url=http://localhost:4566 --queue-url http://localhost:4566/000000000000/annise-local-inventory --profile localstack
aws sqs purge-queue --endpoint-url=http://localhost:4566 --queue-url http://localhost:4566/000000000000/annise-local-sales --profile localstack
- To send a message (Example: this emits the
Sales:created
topic in thenotifications
queue). Basically the message should be stringified in themessage-body
param.aws sqs send-message --endpoint-url=http://localhost:4566 --profile localstack --queue-url http://localhost:4566/000000000000/demo-local-notifications --message-body “{\“Subject\“:\“Sales:created\“,\“Message\“:\“{\“quantity\“:30,\“amount\“:10,\“date\“:\“2021-12-01\“,\“vehicleId\“:2,\“userId\“:1}\“}”
NOTE: Sometimes when restarting the container, the services hang indefinitely with the following message. Waiting for all LocalStack services to be ready. If this is the case please remove the container with the following command to fix the issue for now.
docker rm demo_localstack
And restart the localstack container