Heading image

This post is #2 of a multi-part series on the AWS CDK with TypeScript written during Melbourne lockdown #6.

We will explore how we can make use of LocalStack and LocalStack’s wrapper package aws-cdk-local to speed up our local development.

This post will build off the work that in my AWS CDK With TypeScript Foundations blog post.

Source code can be found here.

Prerequisites

  1. Basic familiarity with AWS CDK for TypeScript.
  2. Read AWS CDK With TypeScript Foundations
  3. LocalStack installed
  4. Docker installed and an understanding of .

This blog post will operate as if you have a pro LocalStack account, however it will also work on the free tier.

Getting started

Clone the working repo from the previous blog post and add some files for our LocalStack config.

At this stage, we are ready to add our config and start up LocalStack.

Starting up LocalStack

Add the following to your file:

Most of this config comes from the LocalStack documentation, but it is important to note that is an exported environment variable that I have available at runtime.

You can also add this to a local file.

To startup LocalStack, run and let it does its things.

To check things are running, we can ping the port 4566:

Updating our infrastructure to use AWS CDK Local

Once Docker is up and running, we need to amend our project code to allow for us to use .

To do so, we need to add the following to our file under :

That’s it! The command is a wrapper around that points the actions of the CDK to the local 4566 port used by LocalStack.

Deploying our stack to LocalStack

Everything from our previous post has the code ready to deployed, so we can work through our lifecycle of and but with the caveat that we use the local script:

We are now ready to deploy:

Seeing our new bucket locally

We can now check our bucket was deployed successfully through the AWS CLI. You have two choices:

  1. Use the argument with the AWS CLI to point to .
  2. Use the which will automatically point to .

I am going to use the latter in this demonstration as it is just a thin wrapper around the AWS CLI:

Awesome! We can see our bucket is available locally.

Tearing down our local stack

As per usual, we can go through the process of tearing down the local stack (or simply stopping the Docker containers).

For now, let’s get into the habit of a proper teardown via the CDK:

Summary

Today’s post demonstrated how to use LocalStack to work on a local AWS environment.

LocalStack significantly improves development time for infrastructure (where supported) and is a great tool all around with an active community.

I highly recommend giving it a go. I will be using LocalStack behind the scenes where I can for the upcoming blog posts to test locally.

Resources and further reading

  1. AWS CDK for TypeScript
  2. LocalStack
  3. AWS CDK With TypeScript Foundations
  4. LocalStack installed
  5. Docker installed
  6. Working with the TypeScript AWS CDK blog series

Photo credit:

Originally posted on my blog. To see new posts without delay, read the posts there and subscribe to my newsletter.

I write content for AWS, Kubernetes, Python, JavaScript and more. To view all the latest content, be sure to visit my blog and subscribe to my newsletter. Follow me on Twitter.

Senior Engineer @ UsabilityHub. Formerly Culture Amp.