No fuss no muss TypeScript Lambda with CDK

One of the great things about AWS Lambda is the flexibility to develop functions in a bunch of different languages like Python, Node.js, Go and some others. CDK allows us to easily create and maintain these functions (among other things) but if you need to build any assets or use typescript for Lambda you’ll need to handle this before you can deploy the function. This isn’t uncommon and many folk would be doing this already in pipelines etc but there is a super easy way to do this in CDK that might just come in handy…

AWS CDK Lambda

Creating a new Lambda function in CDK is pretty straight forward, it only takes a few lines of code:

new lambda.Function(this, "Function", {
  runtime: lambda.Runtime.NODEJS_18_X,
  handler: "index.handler",
  code: lambda.Code.fromAsset(path.join(__dirname, "lambda-handler")),


This of course will work perfect fine for any Lambda’s that you’ve written that don’t need any further build steps or you’ve already built.

But what if…

The other way with typescript

The nodeJsFunction CDK construct (available in the aws-lib) provides build capability with esbuild. I recommend checking out the documentation it goes into great detail covering a bunch of different scenarios.

If you follow the construct way of structuring and naming your files you can create a very simple typescript lambda with a few lines of code, like the previous example but this also builds:

lambdaFunction = new lambda.NodejsFunction(this, "demo", {
  functionName: "talkncloud-demo-function",

The above code snippet will build the typescript lambda if it’s located in the same directory as your stack and follows the same naming convention with .id on the end. It is also expected that the handler is named handler, you’ve probably seen this in the other constructs.

Of course, you don’t have to do it this way, you can structure your project however you want, you’ll just need to update the props to your location (see my code example at the end).


To use this method of building you’ll need to either have esbuild installed or docker. If CDK can’t find esbuild it will run a container with everything needed to run the build. Pretty sweet.

There are no extra steps required to run the build, this construct works in nicely with your existing workflow, simply run CDK synth:

cdk synth

CDK Synth will automatically run esbuild with any specific configuration you’ve supplied and generate the build files.


If you want to test your new typescript lambda locally you can easily do this with sam. To this you’ll first need to be authenticated (aws login) and have docker running, for tips on easy authentication tooling check out my previous post. Oh, and you’ll need sam.

sam local invoke demo -t cdk.out/MyStack.template.json

You can find more documentation on locally testing Lambda with sam including testing with events in the official docs.

Note: this assumes you’ve run synth first

Final thoughts

The nodeJsFunction is handy way for you to leverage esbuild that fits in nicely with CDK. The construct has been built in a way that provides a lot of flexible and consideration for different requirements such as mono repos, customization and package dependencies. If you’re already using a different build process or you’re looking for alternative to smooth out the process I recommend checking it out.

I’ve added some sample code to the talkncloud repo, if you’d like to check it out it will deploy a working lamdba function written in typescript using the construct mentioned in this post.

{{< bookmark url="" title=“talkncloud/aws” description=“all things cloud from IaC to full apps. Contribute to talkncloud/aws development by creating an account on GitHub.” icon="" author=“talkncloud” publisher=“GitHub” thumbnail="" caption="" >}}

Are you already using this construct, how’s it going for you, any tips?

Credits: Article photo by Jametlene Reskp on Unsplash

Related Posts

AWS official Lambda layers with AWS CDK

This is my first official post since joining the AWS community builders and well it’s not super exciting but none the less it was a source of frustration for me so it might be for others…lambda layers.

Read more
Project templating for cloud

    Project templating for cloud

    So you’ve started using AWS CDK, you’ve shipped a few projects, you’ve shared your findings with your team and now they’ve started using CDK…then you start to notice the subtle differences in how developers have created their projects, dot files, style, folder structure and the usage of custom constructs.

    Read more

    AWS Lambda Graviton2, cheap and slow

    I was pretty pumped when I saw the announcement for AWS Lambda Graviton2 support for a couple of reasons, performance and cost.

    Read more