Infrastructure is Code with the AWS CDK - AWS Online Tech Talks was easy to follow but done in Python (nothing bad about it!). While porting the demo project to TypeScript, I came across a few hurdles:
-
@aws-cdk/aws-lambda won't package external dependencies, i.e.: uuid. This problem puzzled (and surprised) me and many others:
-
The official CDK example api-cors-lambda-crud-dynamodb works but it requires building/watching TypeScript before calling
cdk deploy
, which doesn't feel like the best tooling experience. -
AWS documentation Creating a serverless application using the AWS CDK doesn't help either (surprise!).
-
I ended up with the experimental @aws-cdk/aws-lambda-nodejs. This makes deploying Lambda written in JavaScript/TypeScript much easier. The overall experience is similar to serverless framework.
const backend = new lambdaNodejs.NodejsFunction(this, 'handler', { entry: 'lambda/url-shortener.ts', handler: 'handler', runtime: lambda.Runtime.NODEJS_14_X, environment: { TABLE_NAME: table.tableName, }, });
Prerequisite: your CLI should have access to AWS with suitable AWS_PROFILE
and AWS_REGION
.
yarn
- install dependenciescdk bootstrap
- if you haven't got stackCDKToolkit
on CloudFormationcdk deploy '*'
- deploy all stacks to dev environmentENV=prod cdk deploy '*'
- deploy all stacks to production environment