LocalStack provides an easy-to-use test/mocking framework for developing Cloud applications. In today's post, we're going to have a quick look at setting up LocalStack locally to work with their free tier AWS emulated products.
In particular, today's post will have a quick look at emulating S3 buckets for local development.
Before getting started, it is required that you have the following on your machine:
Following the instructions on their README, we can use
pip to install the package.
In my case, I installed using
Once installed, we can get everything up by running
localstack start. You will get feedback in the terminal that a number of services are started and finally get the service list with the ports they are running on. By default, these services will start on port 4566. Easy peasy!
Now that things are running, we can run some basic tests using the AWS CLI. I am not going to go into the installation process for the CLI, so follow the link above and use those installation instructions if you have not yet installed the CLI.
We will use the
aws s3api [command] --endpoint-url http://localhost:4566 pattern to interact and use the local endpoint to list and create buckets.
To show what we currently have, we can run
aws s3api list-buckets --endpoint-url http://localhost:4566 and we will get something like the following:
This proves that we currently have no buckets in our LocalStack setup.
We can create a bucket
my-bucket by running
aws s3api create-bucket --bucket my-bucket --endpoint-url http://localhost:4566.
To confirm it has been made, we can run
aws s3api list-buckets --endpoint-url http://localhost:4566 once again:
Create a new
temp.txt and add some content. In my case, I added
Once this is done, we can put the file
temp.txt into bucket
my-bucket by running
aws s3api put-object --bucket my-bucket --key temp.txt --body temp.txt --endpoint-url http://localhost:4566. You will have an object returned with an
ETag value if successful.
To confirm our object is now in the bucket, run
aws s3api list-objects-v2 --bucket my-bucket --endpoint-url http://localhost:4566.
If we remove our
temp.txt file locally using
rm temp.txt, we can retrieve the bucket version by running
aws s3api get-object --bucket my-bucket --key temp.txt --endpoint-url http://localhost:4566 temp2.txt.
Once successful, you can run
cat temp2.txt to see that we installed the bucket's
temp.txt file into
Now that we have proven how to develop and test the S3 service locally, we can terminate our
localstack instance in the terminal to tear down the local infrastructure.
In summary, we installed LocalStack to our local machine and explored one of the free-tier services in S3.
I am working locally with interacting with some AWS services through some language APIs, and the capability to do so locally with LocalStack has been a real time saver for putting together some infrastructure solutions for my personal projects.
I highly recommend giving it a go for your own infrastructure development. There may be some issues that I am yet to run into, but as far as a lightweight approach to quickly interacting with files locally and not expending costs, it has been great for introductions into some of the AWS products.
Image credit: REVOLT