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?)
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
- General CDK best practices: https://github.com/kevinslin/open-cdk
- Background on design of CDK: https://cdkworkshop.com/
- Deep dive into AWS CDK: https://www.youtube.com/watch?v=9As_ZIjUGmY
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
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
npm install -g aws-cdkto install and update CLI tools like cdk globally. This is very different from…
npm install @aws-cdk/aws-ec2to install packages locally in your project and your
- Use a
^before your version number to let npm upgrade packages when you run
npm installin your project, take the
^to pin a version. e.g.
npm run buildto get errors that might not show up in the output of
cdk deploy, but use
tsc --build --cleanafterwards to remove compiled .js fles — otherwise
cdk deploymay 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 installto 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.
Those are the tips I wish I’d known when I started using CDK. Best of luck!