DEV Community

Cover image for Local AWS development with LocalStack
Dennis O'Keeffe
Dennis O'Keeffe

Posted on • Originally published at blog.dennisokeeffe.com

Local AWS development with LocalStack

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)
  • pip (python package manager)
  • Docker
  • AWS CLI (for interacting with LocalStack after installation)

Installing LocalStack

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

# if you are using pip
pip install localstack
# or if using pip3
pip3 install localstack
Enter fullscreen mode Exit fullscreen mode

In my case, I installed using pip3.

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:

{
  "Buckets": [],
  "Owner": {
    "DisplayName": "webfile",
    "ID": "<some-id>"
  }
}
Enter fullscreen mode Exit fullscreen mode

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:

{
  "Buckets": [
    {
      "Name": "my-bucket",
      "CreationDate": "2021-01-12T05:46:28+00:00"
    }
  ],
  "Owner": {
    "DisplayName": "webfile",
    "ID": "bcaf1ffd86f41161ca5fb16fd081034f"
  }
}
Enter fullscreen mode Exit fullscreen mode

Success!

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.

{
  "Contents": [
    {
      "Key": "temp.txt",
      "LastModified": "2021-01-12T05:51:38+00:00",
      "ETag": "\"746308829575e17c3331bbcb00c0898b\"",
      "Size": 14,
      "StorageClass": "STANDARD"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

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.

{
  "AcceptRanges": "bytes",
  "LastModified": "2021-01-12T05:51:38+00:00",
  "ContentLength": 14,
  "ETag": "\"746308829575e17c3331bbcb00c0898b\"",
  "CacheControl": "no-cache",
  "ContentEncoding": "identity",
  "ContentLanguage": "en-US",
  "ContentType": "binary/octet-stream",
  "Metadata": {}
}
Enter fullscreen mode Exit fullscreen mode

Once successful, you can run cat temp2.txt to see that we installed the bucket's temp.txt file into temp2.txt locally.

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
  2. AWS CLI - Install

Image credit: REVOLT

Originally posted on my blog. Follow me on Twitter for more hidden gems @dennisokeeffe92.

Top comments (0)