Home > Build AWS Lambda Function in Typescript CDK
Build AWS Lambda Function in Typescript CDK
The first two reasons are enough to migrate all available lambda function to typescript.
To read rest of the article please Purchase Subscription
Luckily AWS CDK has all the settings so you just focus on writing typescript lambda code and everything else is handled by CDK.
Before we get started I would recommend to develop CDK project inside development docker container.
So we no longer have the issue of "this works on my computer". And it will not mess with your existing workflow.
So checkout my blog post for detailed video explanation. Then checkout CDK development container settings on github
Important create new CDK project and install following dependencies
npm i esbuild @aws-cdk/aws-lambda-nodejs
Optionally you can also install
npm i @aws-cdk/aws-lambda for the
So first create lambda function as follows
mkdir -p lambda-fns/hello-world/
npm init -y
npm i -D @types/aws-lambda @types/node typescript
So we have created folder
lambda-fns for all lambda functions. And our first function is
Then we open that folder with
$_ command. Its variable for last word of last command. In our case it is
Then we need to install type definition so initialize with
npm init and install typescript, types for
lambda and node.
index.ts and write following code
We have simple function responding hello message. But here we have advantage of type safety.
Now add following code in the
When creating function you just need to provide the entry path for function and handler name. By default the runtime is node 12.x.
If you want to modify
esbuild options then inside bundling you can change options.
The main benefit of this pattern is you don't have to write the
It already has default
tsconfig but if really want to override it then you have option to specify
tsconfig for each function.
Now you can run lambda locally and test it.
Because of aws sdk v3 for Nodejs written in typescript this is the right time to move all of your lambda functions into typescript.
And its very easy to do with AWS CDK NodeJSFunction pattern.
Free users cannot comment below so if you have questions then tweet me @apoorvmote. I would really like to hear your brutally honest feedback.
If you like this article please consider purchasing paid