Debugging Typescript AWS Lambda with VSCode and Serverless

Enrico Portolan
3 min readJun 26, 2019

--

Do you want to step through code running live in Lambda? Are you struggling reading console.log in Cloudwatch? Keep reading then.

This guide will help you debugging your AWS Lambda Typescript project.

1. Use Typescript with AWS Lambda + Serverless

Project setup

Go to your projects folder and create a new serverless project using serverless create --template aws-nodejs --path hello-ts-lambda. Then, navigate to hello-ts-lambda and run npm init -y to initiate a package.json file.

Typescript setup

Install typescript globally so it can be referenced in the CLI by running npm i -g typescript. With that installed globally, tsc -v should output Version 3.1.x. Initialize typescript in the project by running: tsc --init. This will create the tsconfig.json file that will control language features, modules, transpilation, output pathing, etc… To learn more about these configurations, check out the typescript docs.

Edit the file to look like this:

Let’s go through all the options:

  • target: specifies the ECMAScript version to target, which determines the language features to support.
  • module:this specifies the module system to transpile to. In node, that’s going to be CommonJs.
  • sourceMap:this specifies the module system to transpile to. In node, that’s going to be CommonJs.
  • outDir:output structure to the directory.
  • rootDir: list of root folders whose combined content represents the structure of the project at runtime.

Note: using the serverless typescript plugin (more on this later), the outDir and rootDir options cannot be overwritten. You have to use .build as outDir and ./ as rootDir.

Serverless Local Invocation

The Serverless Framework has a really handy command for invoking a function locally if it’s written in NodeJS or Python. The command sls invoke localruns your code locally by emulating the AWS Lambda environment.

First things first, install serverless as dev dependency. This is to ensure that CI tools (or other developers) can use the Serverless framework for deployment without having to install it themselves and to exclude from the deployment package so it wouldn’t add to your deployment size.

npm i -D serverless

In order to use Typescript with AWS Lambda, you need to install the serverless plugin to parse typescript(https://github.com/prisma/serverless-plugin-typescript), @types for NodeJS and Typescript:

npm i -D @types/node
npm i -D typescript
npm i -D serverless-plugin-typescript

Lastly, add the following plugin to your serverless.yml:

plugins:
- serverless-plugin-typescript

2. Add Debug configuration in VSCode

Add this in the launch.json file:

Please be careful to use .build as outFiles directory because the outFiles and rootDir options cannot be overwritten in the serverless-typescript plugin.

Happy debugging :)

Bonus options

  • if your function depends on environment variables, then you can set those up in the launch.json config file.
  • if your function needs to access other AWS resources, then you can use the following plugin: https://github.com/mhart/awscred. Please, don’t ever leave AWS credentials in plain text in the launch config. Seriously…

I hope you find this article useful. If you have comments, suggestions and ideas, please leave them below in the comments section.

All the code is available here: https://github.com/enricop89/aws-lambda-typescript-debug

You can follow me on Twitter for other blog posts

--

--

Enrico Portolan

Passionate about cloud, startups and new technologies. Full-stack web engineer