Skip to content

Instantly share code, notes, and snippets.

@brunopk
Last active December 25, 2023 21:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunopk/d8964d983f3adeb45345bf442e8d1aff to your computer and use it in GitHub Desktop.
Save brunopk/d8964d983f3adeb45345bf442e8d1aff to your computer and use it in GitHub Desktop.
Debugging Typescript AWS Lambda with VSCode and Serverless

Debug TypeScript serverless application in VSCode

  1. Edit serverless.yml, the handler path should be from the root folder (not dist/**/your_handler.main or from any directory with compiled files), for instance:
    ...
    my_function:
     handler: handlers/my_function.main
    ...
  2. Edit serverless.yml adding serverless-plugin-typescript to the plugin section:
    plugins:
      - serverless-plugin-typescript
    ...
  3. Install the plugin and some other NPM dependencies:
    $ npm install --save-dev typescript
    $ npm install --save-dev serverless-plugin-typescript
    
  4. Edit your launch.json with something like this:
     {
       "version": "0.2.0",
       " configurations": [
         {
           "type": "node",
           "request": "launch",
           "name": "Whatever you want",
           "program": "${workspaceFolder}/node_modules/serverless/bin/serverless",
           "args": [
             "invoke",
             "local",
             "--function",
             "my_function",
             "--path",
             "mocks/my_function.json"
           ],
           "sourceMaps": true,
           "preLaunchTask": "tsc: build - tsconfig.cjs.json",
           "runtimeArgs": ["--lazy"],
           "protocol": "inspector",
           "runtimeExecutable": "node",
           "env": {
           },
         }
       ]
     }
  5. Check the VSCode debug console to see if it is working.

Notes:

  • Adding "outFiles": ["${workspaceFolder}/dist/**/*.js"] to the launch.json avoid errors like Could not read source map for file://..../node_modules/typescript/lib/typescript.js.map' but it probably won't hit breakpoints.

  • More information : https://enrico-portolan.medium.com/debugging-typescript-aws-lambda-with-vscode-and-serverless-967f2a201fab.

  • To debug without mocks for instance to invoke endpoints from Postman, edit your launch.json with something like this:

      {
        "version": "0.2.0",
        " configurations": [
          {
            "type": "node",
            "request": "launch",
            "name": "Whatever you want",
            "program": "${workspaceFolder}/node_modules/serverless/bin/serverless",
            "args": ["offline"],
            "sourceMaps": true,
            "preLaunchTask": "tsc: build - tsconfig.cjs.json",
            "runtimeArgs": ["--lazy"],
            "protocol": "inspector",
            "runtimeExecutable": "node",
            "env": {
            },
          }
        ]
      }

    and remove serverless-plugin-typescript from the serverless.yml (also the change all handlers path on the serverless.yml as mentioned before)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment