DEV Community

Matti Mårds
Matti Mårds

Posted on

AWS CDK and asynchronous requirements

I have moved my infrastructure definitions to TypeScript and enjoy having the entire stack in the same language.

For the Lambda functions, I want to compile and bundle the code at deployment.

I started out defining the stack using inheritance, as the CDK documentation's "Getting Started" page. This quickly showed some difficulties, as Webpack's compiler runs asynchronously, and object constructors do not.

I changed over to a more functional approach of building the stack. Instead of inheriting from cdk.Stack, I instantiate it and pass the object to resources as normally.

I have taken the same approach on a couple of custom CloudFormation resources with success. Now I am looking forward to the future of programming my AWS stack and not statically defining it.

Top comments (3)

Collapse
 
idvb profile image
Dan Van Brunt

Hey Matti,
Can you share that rest of the setup? eg. your packCode function....
Also, how are you solving the function version creating so that its only when the bundled code has changed?

Collapse
 
mamaar profile image
Matti Mårds

Hi, Dan!
Sorry for the late reply, I don't visit dev.to very often.

I have moved away from my solution, as CDK now have a aws-lambda-nodejs module: docs.aws.amazon.com/cdk/api/latest...

Collapse
 
svaterlaus profile image
Spencer Vaterlaus

Love it! Now I can actually compose my constructs and get around some of CDK's quirks. 😃