No fuss no muss TypeScript Lambda with CDK
Tags: aws, typescript, lambda
Published: 2023-03-18 04:25:29

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:

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

Source: [https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Function.html](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Function.html "https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Function.html")

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](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda_nodejs-readme.html "aws nodejsfunction") 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:

```typescript
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).

#### Building

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:

```shell
cdk synth
```

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

#### Testing

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](https://www.talkncloud.com/aws-sso-multiple-accounts/ "easy sso auth"). Oh, and you'll need [sam](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html "sam cli install").

```shell
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](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-cdk-getting-started.html "aws sam 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.

https://github.com/talkncloud/aws/tree/main/lambda-nodejs

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

Credits: Article photo by [Jametlene Reskp](https://unsplash.com/@reskp?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/photos/W36m2sQkjzM?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)