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
- Basic familiarity with AWS CDK for TypeScript.
- Read AWS CDK With TypeScript Foundations
- LocalStack installed
- Docker installed and an understanding of
docker compose
.
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 docker-compose.yml
config and start up LocalStack.
Starting up LocalStack
Add the following to your docker-compose.yml
file:
Most of this config comes from the LocalStack documentation, but it is important to note that LOCALSTACK_API_KEY
is an exported environment variable that I have available at runtime.
You can also add this to a local .env
file.
To startup LocalStack, run docker compose up
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 aws-cdk-local
.
To do so, we need to add the following to our package.json
file under scripts
:
That’s it! The cdklocal
command is a wrapper around aws-cdk
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 synth
and deploy
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:
- Use the
--endpoint
argument with the AWS CLI to point tolocalhost:4566
. - Use the
awslocal package
which will automatically point tolocalhost:4566
.
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
- AWS CDK for TypeScript
- LocalStack
awslocal package
- AWS CDK With TypeScript Foundations
- LocalStack installed
- Docker installed
- Working with the TypeScript AWS CDK blog series
Photo credit: rishabh
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.