Rescue Your Soul From AWS CDK Dependency Hell and Improve Your DevEx With 3 Musketeers
Tags: cdk, aws, iac, 3musketeers
Published: 2022-02-22 19:14:17

Cross post: [Medium](https://medium.com/p/4293815d01a8) | [Arcadian](https://arcadian.cloud/aws/2022/02/22/make-your-life-easier-by-running-aws-cdk-in-a-docker-container/)

_GitHub Repo: <https://github.com/chrishart0/aws-cdk-typescript-3-musketeers>_

This is an example of containerized AWS CDK following the 3 Musketeers pattern.

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

Links:
* 3 Musketeers for an epic Developer Experience
* What is 3 Musketeers?
* What is 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.

## 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: <https://github.com/chrishart0/aws-cdk-typescript-3-musketeers>

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_](https://github.com/chrishart0/aws-cdk-typescript-3-musketeers/tree/master/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](https://github.com/chrishart0/aws-cdk-typescript-3-musketeers/blob/master/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](https://github.com/chrishart0/aws-cdk-typescript-3-musketeers/tree/master/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, simply skip running the bootstrap command_

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`

[Gif of make deploy running](https://miro.medium.com/max/700/1*XeZSVbCmVtuq-PbS8Juf2A.gif)

## 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:

* Copy the `Makefile`, `Dockerfile`, and `docker-compose.yaml` files into the root of your project
* Update the `CDK_PATH` variable in `three_musketeers.env` and `Makefile`
* Run make to do initial setup and output cdk synth to verify everything is configured right
For more information refer to the [README](https://github.com/chrishart0/aws-cdk-typescript-3-musketeers) on the GitHub repo. I’d love to hear any thoughts, comments, or critiques in the comments below.