Image for post
Image for post

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.

Prerequisites

Before getting started, it is required that you have the following on your machine:

  • Python (both Python 2.x and 3.x supported)

Installing LocalStack

Following the instructions on their README, we can use pip to install the package.

Running LocalStack

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!

Creating our first S3 bucket with LocalStack

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:

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:

Adding and removing objects locally

Create a new temp.txt and add some content. In my case, I added Hello, world!.

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.

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.

Conclusion

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.

Resources and further reading

  1. LocalStack — GitHub

Image credit: REVOLT

Originally posted on my blog.

Senior Engineer @ Culture Amp. Tinkerer and professional self-isolator in 2020.

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