Skip to content

Instantly share code, notes, and snippets.

@desbo
Last active July 18, 2019 10:25
Show Gist options
  • Save desbo/f225802327abf2eee86b3a1d81a2219a to your computer and use it in GitHub Desktop.
Save desbo/f225802327abf2eee86b3a1d81a2219a to your computer and use it in GitHub Desktop.
CDK notes

CDK notes

Notes on building a small application (two lambdas and a step function state machine) with the CDK (TypeScript).

directory structure

cdk init can only be run in an empty directory. The name of this directory is used throughout the generated code, notably as the name of the generated stack. It seems like the intention is to use the root directory of the project for the CDK code as well as any other source code. I preferred to separate the CDK code from Scala, so I put it into its own subdirectory that was ultimately named cdk, but when running cdk init I temporarily gave it the same name as my project so the generated stack name made sense (e.g. MyProjectStack instead of CdkStack).

constructs

CDK applications are made by composing constructs. A construct is a representation of an AWS Resource at varying levels of abstraction. Some are auto-generated and therefore as low-level as CloudFormation itself, others provide a simplified "intent-based" API, and at the highest level there are "patterns" that model combinations of related services (e.g. a lambda/API gateway application).

Many AWS resources are already available at a higher level of abstraction than simply mirroring CFN properties, and they're easily discoverable in the Construct Library and installed with yarn/npm. Having types and the ability to explore these resources in IntelliJ is a transcendentally better experience than writing plain YAML.

In CDK terminology, Constructs are placed within Stacks (equivalent to a CFN stack) and one or more Stacks constitute an App. Constructs can be custom built rather than imported from AWS too.

deployment and riff-raff

The CDK CLI has deployment commands and (for example) will upload local lambda assets to S3 if you've configured your function to do that. I'd like to be able to use these features without going through the rigmarole of creating riff-raff configs, uploading artifacts etc. For that to work, I guess the process would be to generate a CFN template with the CDK on TeamCity and then treat that as a standard CFN deployment. I'd also have to set up my lambda code deployment and work out how to provide the S3 path riff-raff uploads them to to my CDK stack. It's a lot of extra overhead and the benefits are not obvious to me. We need to make it as easy as possible to use deployment tools like CDK and others like it.

generating CFN templates

This is done with cdk synth, although in my TypeScript CDK project I had to first run npm run build to compile the JS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment