Localstack, Our Local AWS Hero

Localstack, Our Local AWS Hero

This article covers the main questions about Localstack and a basic quick start guide on how to use Localstack

Overview

With the increase in demand for the knowledge of AWS in the tech space, developers rush on to AWS to open free tier accounts to learn the ins and outs of this cloud service provider. However, this free tier account is usually exhausted almost immediately due to some factors.

That still shouldn't stop our hands-on learning of AWS. A solution has been built to help alleviate this problem, enabling us to surpass these issues and also bringing down the comforts of AWS to our local machine. This tool is Localstack.

What is Localstack?

Localstack is a mock engine/testing framework for developing cloud applications. It creates a testing environment on our local machines that replicates the same functionalities and APIs, similar to that of AWS.

How Does Localstack Work?

Localstack achieves this cloud emulation by creating a container on our local machine in which it provisions any cloud resource required, and yes I mean any cloud resource, for example, lambda functions, API gateways, S3 buckets, Dynamo databases, and much more.

Note: These “resources” that are being provisioned can't be accessible to anyone, it is only available locally.

Who Is Localstack For?

Anyone can use Localstack, from beginners to experts.

If you are starting out in learning AWS and cannot continue using the free tier account because you have exhausted its limits, Localstack is the way to go.

Additionally, intermediates and experts can also use Localstack for testing their applications that use cloud resources, making sure everything works well locally before deploying their applications to the main cloud.

Why use Localstack?

There are a ton of advantages you get when you use Localstack, some of which will be highlighted below:

  • It is beginner-friendly and provides us with a way to get familiar with AWS locally.
  • It increases efficiency and provides a great testing and development environment
  • It helps save costs, as you're not actually creating new resources on AWS
  • It improves the quality of code shipped off into production through continuous integration tests.
  • It also accelerates the rate of production of applications.

Localstack Quick Start

To get started with Localstack, we need to install it first on your local machine. There are different methods to accomplish this, but I will be going through the easiest method I could find, i.e installing Localstack using python.

To get started, make sure you have python3 and pip installed.

Now we open our terminal and enter this command:

$ python3 -m pip install localstack

If the command ran successfully, it shows that we have installed LocalstackCLI. You can check by running this command:

$ localsack --help
Usage: localstack [OPTIONS] COMMAND [ARGS]...
  The LocalStack Command Line Interface (CLI)
Options:...

You should see the output above with it too.

With LocalstackCLI installed, let us see how we can create an S3 bucket and store a file in it.

Firstly, we need to start Localstack, we can do this by running:

$ localstack start

This will take some time depending on your machine. When it is finished you would see a Ready statement at the end of the process.

Now to create an S3 bucket with Localstack, we can do that using this command:

aws --endpoint-url='http://localhost:4566' s3api create-bucket --bucket my-bucket

Next, you should see an output like this:

----------------------------------------------------------------------
|                            CreateBucket                            |
+----------+---------------------------------------------------------+
|  Location|  http://my-bucket.s3.localhost.localstack.cloud:4566/   |
+----------+---------------------------------------------------------+

This shows us the location of our bucket.

Now, let’s add an image to our bucket. We can do that by using this command format:

aws --endpoint-url='http://localhost:4566' s3api put-object --bucket <bucket-name> --key <object-key-name> --body <object>

Replacing bucket name with our actual bucket name and the object with any file we want to add, we will have this:

aws --endpoint-url='http://localhost:4566' s3api put-object --bucket my-bucket --key first-object --body hello.txt

After running this command, we have successfully added a file called hello.txt to our bucket. We can list the content in our bucket using this command:

aws --endpoint-url='http://localhost:4566' s3api list-objects --bucket my-bucket

We should get an output like this:

--------------------------------------------------------------------------------------------------------------
|                                                 ListObjects                                                |
+------------------------------------------------------------------------------------------------------------+
||                                                 Contents                                                 ||
|+-------------------------------------+---------------+----------------------------+-------+---------------+|
||                ETag                 |      Key      |       LastModified         | Size  | StorageClass  ||
|+-------------------------------------+---------------+----------------------------+-------+---------------+|
||  "d41d8cd98f00b204e9800998ecf8427e" |  first-object |  2022-05-21T16:19:42.000Z  |  0    |  STANDARD     ||
|+-------------------------------------+---------------+----------------------------+-------+---------------+|

There you have it, how to create an s3 bucket and store a file in the bucket using Localstack.

Conclusion

All in all, Localstack is a great tool to start working with today, with the amazing features it offers and problems it solves, it would really be a great tool to have in your arsenal as a developer.

If you choose to have more insights on Localstack, you can find their documentation quite beginner-friendly.

Here are some other resources that go in-depth on the functionalities of Localstack: