How To Run AWS On Your Local Machine ?
In this practical article we will see how we can bring the AWS to our local machine and we will practice using it with the CLI client.
Why Locally?
There are several reasons you may want to run AWS stacks locally, Some of the frequent ones are:
- Testing your application locally which requires AWS services
- Playing with AWS services and learn how to use them without any cost
- Debugging Locally without breaking production services
- Simulation of a production scenario on your local machine
Prerequisites
Before jumping into the fun part there are a few things you need to be familiar with, Like some tools and concepts which I cover here quickly.
Docker
If you already know about Docker, Nice, you are one step ahead, If not; you don’t need to know much about docker because we will only use it to Run our Local AWS container which contains the services, you only need to have it installed and run some commands on it which I will mention them here.
If you don’t have the Docker installed check the Docker installation page to install it on your local machine.
* AWS CLI *
Even if you already have the AWS CLI installed don’t skip this section and follow it step by step, Since you need the configuration part to not to break anything on your real AWS account.
To interact with the AWS services running on your local machine you need to install AWS CLI and then configure your profile, the easiest way to install it is using “pip3” by running the below command:
pip3 install awscli --upgrade --user
Now you need to configure your profile, you can do this by running the aws configure --profile local
command, after running this command you need to provide some informations which the values for them matter when you want to setup your real AWS account on your cli, for local setup you can give the below values:
AWS Access Key ID [None]: dummy
AWS Secret Access Key [None]: dummy
Default region name [None]: us-west-1
Default output format [None]: json
To confirm you have configured your AWS profile successfully you can check the contents of the .aws/config
and .aws/credentials
files. you can check them by cat .aws/config
and cat aws/credentials
command, the contents should look like below:
AWS SQS
SQS is one of the AWS most frequently used services which we will also use it in our case study.
This one is also not a big deal, If you have not worked with it before you can just consider it as a simple queuing and messaging tool or an event bridge, something like RabbitMQ or Kafka (They are of course all different from each other and have their own features but they can be used for the same purposes).
Installation
Now we are about to start with the fun part, First of all you need to have Docker installed and running on your local machine.
Now let’s run A lovely container on our docker which contains the AWS services, To do so; run the below command:
docker run -p 4567-4584:4567-4584 -p 4566:4566 localstack/localstack
After starting the LocalStack container you should be able to see the running AWS services on your local machine using the endpoint: http://localhost:4566/health
The endpoint shows the service with their status in a json format.
So far so good! Now you have a compact AWS running on your local machine with the services listed in the health endpoint, Congrats!!!🎉
Let’s Play
Now let’s have some fun and use the SQS service to validate and test our local AWS.
Create A New Queue
Run the below command to create a new queue called payment:
aws --profile local --endpoint http://localhost:4566 sqs create-queue --queue-name payment
Note that when you interact with your local AWS you should always provide the “endpoint” flag(the value for it is: http://localhost:4566), which indicates you are going to connect to the local AWS, Specially when you also have your real AWS account configured on your local machine, Also always use the “profile” flag to define you are working with the local, Having the “endpoint” flag is a necessity while “profile” is something nice to have which makes your life easier.
The output of this command is the queue url in a json format which looks like below:
{
"QueueUrl": "http://localhost:4566/000000000000/payment"
}
The SQS queue works with the producer and consumer concept, which means one of its client sends some messages to the queue and the other consumes those messages from the queue.
Send Message to the Queue
Now we want to send a message to the payment queue with the below command:
aws --profile local --endpoint http://localhost:4566 \
sqs send-message --queue-url http://localhost:4566/000000000000/payment \
--message-body '{"message_content": "Payment successful", "price":100, "currency": "€", "id":7}'
The output of the send-message command is a json which contains the message id. The output of our send-message command should look like below:
{
"MD5OfMessageBody": "6d7b97ccd574481eccff4420e2878034",
"MessageId": "e1dca076-b9c7-4e24-8bba-5c3ec70714d8"
}
Note that the values for “MD5OfMessageBody” and “MessageId” will be different on your local machine.
Receive Message
Now we want to read the produced message from the queue, This time we are the consumer, Run the below command to receive at most 10 messages from the queue:
aws --profile local --endpoint http://localhost:4566 \
sqs receive-message --queue-url http://localhost:4566/000000000000/payment \
--max-number-of-messages 10
The output should be the message you previously sent to the queue with some extra informations. Received messages should look like below:
{
"Messages": [
{
"MessageId": "e1dca076-b9c7-4e24-8bba-5c3ec70714d8",
"ReceiptHandle": "YTYxMzlkMjQtNjk0OS00YzA3LTk3YzAtNGUwMzMyZmJiMGZiIGFybjphd3M6c3FzOnVzLXdlc3QtMTowMDAwMDAwMDAwMDA6cGF5bWVudCBlMWRjYTA3Ni1iOWM3LTRlMjQtOGJiYS01YzNlYzcwNzE0ZDggMTY3NDc3ODY3MC42MTUwODI3",
"MD5OfBody": "6d7b97ccd574481eccff4420e2878034",
"Body": "{\"message_content\": \"Payment successful\", \"price\":100, \"currency\": \"€\", \"id\":7}"
}
]
}
Congrats!!! You have now used one of the AWS services on your local machine!!! 🎉🎉🎉🍾🍾🍾
You can use other services listed at the “/health” endpoint of your docker container, Locally.
I have more articles about the AWS world and more will come, If you’d care to read more and learn more tricks about AWS, follow me on Medium :)