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.
# Clone and change into the working directory
$ git clone https://github.com/okeeffed/aws-cdk-with-typescript-foundations using-the-aws-cdk-with-localstack-and-aws-cdk-local
$ cd using-the-aws-cdk-with-localstack-and-aws-cdk-local
# Install the modules
$ npm install
# Add aws-cdk-local
$ npm i --save-dev aws-cdk-local
# Create docker-compose.yml file for LocalStack
$ touch docker-compose.yml
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:
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:
"scripts": {
"local": "cdklocal"
},
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:
$ npm run local deploy
> aws-cdk-with-typescript-foundations@0.1.0 local
> cdklocal "deploy"
AwsCdkWithTypescriptFoundationsStack: deploying...
AwsCdkWithTypescriptFoundationsStack: creating CloudFormation changeset...
✅ AwsCdkWithTypescriptFoundationsStack
Stack ARN:
arn:aws:cloudformation:us-east-1:000000000000:stack/AwsCdkWithTypescriptFoundationsStack/ec2cbb64
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 to localhost:4566.
Use the awslocal package which will automatically point to localhost:4566.
I am going to use the latter in this demonstration as it is just a thin wrapper around the AWS CLI:
$ awslocal s3 ls | grep hello-aws-cdk-my-first-bucket
2021-08-07 08:10:53 hello-aws-cdk-my-first-bucket
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:
$ npm run local destroy
> aws-cdk-with-typescript-foundations@0.1.0 local
> cdklocal "destroy"
Are you sure you want to delete: AwsCdkWithTypescriptFoundationsStack (y/n)? y
AwsCdkWithTypescriptFoundationsStack: destroying...
✅ AwsCdkWithTypescriptFoundationsStack: destroyed
$ awslocal s3 ls | grep hello-aws-cdk-my-first-bucket
# no grep output
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.