Make your Life Easier by Running AWS CDK in a Docker Container

Cross post from Medium
Tags: CDK, 3 Musketeers, AWS
This is an example of AWS CDK in a docker container following the 3 Musketeers pattern.

GitHub Repo:

What Is This 3 Musketeers Thing Anyway and Why Would I Want to Do Such a Thing as to Containerize the AWS CDK?


There are a host of benefits to the 3 Musketeers pattern, which mostly center around Developer Experience. 3 Musketeers packages all the dependencies up into one container, making it easy for the whole team to stay up-to-date. The other substantial benefit I see of the 3 Musketeers pattern is just how darn easy it makes it to set up new developers. Simply make sure they install docker, docker-compose, and make (also WSL on Windows). Now setup for new projects is as simple as cloning down the repo and running make install.

If you’ve worked on any number of CDK projects, you’ve probably run into hiccups with dependency management, and you know how difficult these issues can be to fight through, especially for developers new to Node. It should be obvious now how 3 Musketeers can mitigate these by packaging up all the AWS CDK tooling into a docker container.

Getting Started

Now that we’ve seen why it’s a good idea to use the 3 Musketeers pattern with the AWS CDK, let’s jump right into it.

Start by cloning down this repo from GitHub:

This will have everything you need to try out containerized AWS CDK. Let’s go through the contents of this repo first.

Notice that the infrastructure directory is the output of running the command cdk init sample-app — language typescript. This gives us a basic sample app to work with. If you aren’t familiar with the AWS CDK in typescript, the infrastructure/lib/infrastructure-stack.ts file is where the infrastructure to be deployed is located. You will notice an SQS queue and an SNS topic have been defined with the queue subscribed to the SNS topic.

You can use this repo as is or replace the contents of infrastructure with your work.

How to Use this Repo as a Starting Point

Note: You don’t need an AWS account setup to test this locally

Open up the CLI into the root of the repo you’ve cloned down locally.

Run the command make; this will do all the needed setup and run cdk synth from within the docker container.

$ make

If you have never used AWS CDK in your configured AWS account before then you must run the CDK bootstrapper. If you simply want to test this repo out locally you can skip this step.

$ make bootstrap

You are now free to run any other commands available such as cdk diff, cdk deploy, and cdk destroy

AWS CDK in a docker container with 3 Musketeers

Use this in an existing project

If you have an existing project and would like to use this 3 Musketeers pattern then follow these steps

  1. Copy the Makefile, Dockerfile, and docker-compose.yaml files into the root of your project
  2. Update the CDK_PATH variable in three_musketeers.env and Makefile
  3. Run make to do initial setup and output cdk synth to verify everything is configured right

For more information refer to the README on the GitHub repo. I’d love to hear any thoughts, comments, or critiques in the comments below.

Also, checkout my other CDK related posts here:

2 thoughts on “Make your Life Easier by Running AWS CDK in a Docker Container”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.