What the heck is `aws-cdk`?
April 5, 2022 (2 years ago)
A few weeks ago, I had no idea what the aws-cdk
was. "Is it like aws-sdk
?", "Is it a Python script?" After starting to use it, I managed to get a grip on it.
aws-cdk
is a way of programmatically declaring CloudFormation templates. Put another way, aws-cdk
generates CloudFormation templates from code.
Background
To understand the CDK, it is important to understand a bit of the history of infrastructure-as-code with AWS.
At its core, AWS uses something called a 'CloudFormation' template to generate resources within AWS. This includes almost every service that AWS provides, serverless or not, including:
-
Cognito & IAM (user management and roles)
-
DynamoDB & other databases
-
API Gateway
-
EC2 instances (servers)
CloudFormation also allows you to define permissions between the different services as well. It is an incredible system.
The problem
Modern development requires much more than what AWS has traditionally provided. Developers require testing environments (which most AWS products - sans Amplify - don't make clear), and much easier ways to define systems. While CloudFormation is an excellent tool, it is very hard to learn, use, and maintain.
Some of the problems come from documentation, but a great deal of this comes from the incredible verbosity of CloudFormation files, and a bad cli.
SAM
One of the most successful tools to overcome the bad cli and verbosity is SAM. SAM provides both a new spanky cli, and a new, less verbose, template for writing out CloudFormation templates. It's important to note that SAM works well with old-skool CloudFormation templates, so it's probably your best choice for deploying these too.
In theory, it also uses Docker to provide local testing, although the reality is this is very hard to set up (and is outside my ability, that's for sure).
Here's a link to the official examples https://github.com/serverless-projects/aws-sam-examples
AWS CDK
AWS CDK is a completely different approach to SAM, in that instead of providing Infrastructure-as-Code as a templating language, it provides infrastructure as code, e.g. JavaScript, TypeScript, Java, python, etc.
This is excellent, because the documentation and intellisense autocomplete is great (unlike with yaml
files), verbosity is further removed, and in the JS world, the cli and library are in npm. This new technology is a game changer, and allows even large and complex stacks to be created and generated with ease, with native support across developer environments and stages. Also, the cli is further improved in that deploys are only made on stacks that have actually been changed.
Here's the docs for AWS CDK: https://docs.aws.amazon.com/cdk/v2/guide/hello_world.html
Serverless Stack (SST -- not to be confused with 'Serverless'), is a fantastic community-made wrapper around the cdk
, with pretty good support for most products. The wrapper is also thin enough to be used in tandem with cdk
library stacks within the same files, even matching the typing well.
That's all for today, will be making improvements to this post throughout the week!