Home > Categories > Aws cdk > Build AWS Lambda Function in Typescript CDK

Home > Categories > Lambda > Build AWS Lambda Function in Typescript CDK

Home > Build AWS Lambda Function in Typescript CDK

Build AWS Lambda Function in Typescript CDK

Updated:

11 Feb 2021

Published:

28 Dec 2020

Motivation

  1. Now AWS SDK V3 for Javascript has first class typescript support.
  2. Also AWS SDK V3 for javascript is now generally available.
  3. With AWS SDK V3 for javascript you get benefits like HTTP keep alive is default behavior.

The first two reasons are enough to migrate all available lambda function to typescript.

But the big problem with typescript is building and compiling to javascript.

How to make sure your `tsconfig` settings can generate javascript code that can be executed on aws lambda.

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

View typescript lambda Source code 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 Runtime interface.

Esbuild is an extremely fast javascript bundler that we will use to build and compile typescript lambda into javascript.

So first create lambda function as follows

mkdir -p lambda-fns/hello-world/
cd $_
npm init -y
touch index.ts
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 hello-world.

Then we open that folder with $_ command. Its variable for last word of last command. In our case it is lambda-fns/hello-world.

Then we need to install type definition so initialize with npm init and install typescript, types for lambda and node.

And create 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 ts-lambda-stack

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 tsconfig.

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.

View Complete Source code on github

Conclusion

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