Your web browser is out of date. Update your browser for more security, speed and the best experience on this site.

AWS but locally: saving time with LocalStack

Nothing is as frustrating during work as constantly being unable to proceed because your technology isn't cooperating. Yet, that's what developers experience when deploying code to the cloud. Especially when you validate whether something works via trial and error, every minute feels like wasted time. Arne Hendrickx demonstrated at Haxx Summer Edition that it can be different.

Arne, since 2018 a Java Developer at Axxes, presented a talk about LocalStack at Haxx Summer Edition. This tool can be described as a complete AWS cloud stack that runs locally on your computer. It's ideal for testing without needing to deploy to the cloud - it was created by developers who wanted to work on the train without internet access.

Share article
Java

Almost the same as the real AWS

LocalStack is an open-source project written in Python. It provides replacements for most AWS services, including Lambda, SNS, and DynamoDB. "It's a single Docker container with mock implementations of AWS services behind it. LocalStack is only intended for local development or for use in your CI pipeline, not as a real replacement for AWS in production," Arne explained.

The tool has both a free and a professional version. For 15 euros per month per user, you get additional AWS services and features, including a GUI that allows you to easily see what is currently running. However, Arne also mentioned that you can already do a lot with the free version.

"We are working on improving a monolithic application. To avoid peak loads, we initially extracted certain functionalities and put them into Lambda, but we ran into technical limits. It's not easy to start those Lambdas locally and test the entire system. Initially, we came up with cumbersome ways to still try to test it in the cloud, but LocalStack is exactly the tool we need to quickly validate changes."

By being able to use AWS services locally, Arne and his colleagues no longer need to deploy to the cloud to, for example, perform an integration test. This allows them to see what works and what doesn't much faster, leading to increased efficiency.

Localstack docker comose as image

LocalStack in Practice

So far the theory, but how does it work in practice? There are four ways to use LocalStack, but some are more convenient than others. "For example, you could use their own CLI tool. Then LocalStack spins up a Docker container in the background, but that doesn't really add much value. A second way is to manually execute the Docker run command, with 'localstack' as the image name. Even better is to include the Docker Compose file in your repository. This way, all team members can find the file in Git. This is how we use LocalStack. Finally, you can also run a local stack in a Kubernetes cluster using a Helm chart."

When opting for the third option, the Docker Compose file looks as follows. You give your container a name, reference LocalStack for your image, specify the network mode as 'bridge', and list the respective ports. Then, you can choose to list and configure a number of necessary services yourself. In Arne's example, the Lambda Executor is set to 'Local', which means the tool will spin up the Lambdas in the same container. Another option is to spin up additional containers for each Lambda. All the different options can be found in the LocalStack documentation. Finally, you also specify where the data should be written and where the source code is located.

Once this is set up, LocalStack runs in a Docker container, and it's a matter of connecting to it to set up your infrastructure. This can be done using various tools, including Terraform, AWS CLI, or CloudFormation.

The usual tools are designed to interact with the real AWS, so as a developer, you'll need to find a way to point them to your localhost. For AWS CLI, for example, you do this by providing an additional parameter 'aws-endpoint-url' with your command.

Once the infrastructure is set up, you can start working with your code. The first code snippet is written in Java and is used to create an SNS Client. In the EndpointConfiguration, you reference localhost instead of the real AWS. Similarly, the second code snippet creates a DynamoDB Client, but in Python, following the same pattern.

Visual Insights v4 01

During his presentation, Arne demonstrated how he could create a simple flight tracker using LocalStack. The basis for this is a DynamoDB containing all the location events of the aircraft. Two AWS Lambdas are attached to this: one to create locations and another to retrieve the latest location. An API Gateway is set up for this purpose, accessible via a POST request to create an event and a GET request to retrieve the latest event. For the latter, a simple frontend is provided, and data about the fictitious aircraft is entered using Postman, a tool for making API calls.

Through this flow, your aircraft is assigned an ID in DynamoDB, and you can see the corresponding geographical coordinates for each timestamp indicating the location where the aircraft was flying at that moment. If you want to test the API Gateway of LocalStack, there's little difference compared to using the real AWS. The only difference is that you need to include "user_request" in your URL. Interested in the code used for the demonstration? You can find it on GitLab.

Arne Hendrickx

Arne Hendrickx

Axxes