Tips and tricks for getting started in CDK (a.k.a. IaC is great! Daheck is typescript?)

I’ve been doing IaC in AWS for years now, primarily through Cloudformation and scripting languages like powershell and python. IaC is great, but these tools have some short comings like yaml being very verbose, and the powershell AWS SDKs not handling idem potency for you.

Because of those short comings, I was very excited when AWS released AWS CDK a little over a year ago now. It cuts down on a lot of copying and pasting yaml files like you do in cloudformation, and gives you easier access to higher level language constructs like loops and if statements. It also comes with options for strong typing, which lets you get compile time errors instead of deploy time errors (anyone watched a complex cloudformation stack deploy for 30 minutes, only to have a syntax error on the last resource and watch it all role back?)

From a very high level typescript is a superset of javascript, and adds some nice features like strong typing. CDK is a framework and CLI for managing AWS resources.

All of that sounds great, but it can be intimidating to start using a new framework and a new language at the same time. I was new to typescript when I started using CDK, so I figured I’d share some of the tips and tricks I learned along the way. I know CDK supports other languages, but we found that most of the examples are in typescript, so we wanted to stay as mainstream as possible.

Do some reading before you start 

AWS is strongly motivated to make it easy for you to get started in CDK. From their perspective the easier it is for you to create resources, the easier it is for you to spend money with them. So before you start hacking away take a few minutes to read some helpful resources like

A few minutes of background research can help you frame up how your project should look, and save you a lot of time once you’re started.

Start with and modify an example project

If you’re new to typescript and CDK start by getting a small example project up and running. There are a ton of good examples in this github repoaws-samples/aws-cdk-examples
You can’t perform that action at this time. You signed in with another tab or window. You signed out in another tab or…github.com

If you pick one of the serverless examples you can usually stay in the free tier to avoid paying for your learning.

If you’re really trying to avoid spending money use

cdk synth

while you’re experimenting to make sure you can produce a valid cloudformation template without actually creating any resources. This is a good way to test changes and get fast feedback

Get familiar with a few key npm features

I’m not (and don’t really want to be) a nodejs developer, but if you’re working in typescript it will help to know some basic NPM commands like

  • Use npm install -g aws-cdk to install and update CLI tools like cdk globally. This is very different from…
  • Using npm install @aws-cdk/aws-ec2 to install packages locally in your project and your package.json file
  • Use a ^ before your version number to let npm upgrade packages when you run npm install in your project, take the ^ to pin a version. e.g. "@aws-cdk/aws-ec2": "^1.20.0" vs "@aws-cdk/aws-ec2": "1.20.0" 
  • Use npm run build to get errors that might not show up in the output of cdk deploy , but use tsc --build --clean afterwards to remove compiled .js fles — otherwise cdk deploy may not pick up your changes
  • If you need to downgrade your CDK package version for any reason you can pin the version and then run an npm install to get to an older version

Take advantage of itellisense in VS Code (or your editor of choice)

One of the main advantages of CDK is it’s ability to predict what your typing based on strong types. If you declare types for your objects like

const gateway_nlb_tg:elbv2.NetworkTargetGroup = new elbv2.NetworkTargetGroup....

your editor will be able to offer you methods and their parameters as you’re working

Get ready to be active in the CDK open source community

You’re using a pretty new open source project, and your best bet for help when you run into issues (in my experience) is to open an issue in the github project.

Keep in mind you’re not opening a support ticket with a vendor, so you need to bring all of the information you can to help some else help you solve your problem (e.g. the versions of CDK and packages you’re using, what you expected to happen, the errors you got, etc).

As long as you open an issue with a reasonable amount of information the community is very willing to help.

Conclusion

Those are the tips I wish I’d known when I started using CDK. Best of luck!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s