Logo
Header Image

Setting Up VS Code to debug a Serverless function

logo

Setting Up VS Code to debug a Serverless function

Chad Faurie

Mar 11, 20192 min read

Debugging Serverless Applications can be a bit of a headache...

We use NodeJs a lot, we also use a lot of CI/CD pipelines to automate the tedious stuff for us, so this brings us to a point where we need to debug things locally because if you are reading this article I am sure you know how much of a pain it is to debug things on AWS lambda.

Current workflow

  • Run Serverless Deploy
  • Test APIGateway
  • Check Cloudwatch Logs
  • Scratch through cloudwatch to find the correct log

By the end of all of this you are pretty frustrated.

The solution is serverless-offline and a bit of configuration, install serverless offline using

npm

npm install -D serverless-offline

yarn

yarn add -D serverless-offline

Then include it in your serverless.yml under plugins along with your other plugins, I added serverless-webpack for my TS project

# Add the serverless-webpack plugin
plugins:
  - serverless-webpack
  - serverless-offline

Once this is done setup your package.json scripts to make a life a little easier.

"scripts": {
    "start": "serverless offline -s dev",
    "test": "mocha"
},

To configure the debug environment within VS Code, on the left hand side click the debug menu items as highlighted below and then click the gear at the top

debug serverless

VS Code will then open a launch.json file which will allow you to configure your debug environment

The snippet below assumes that you have serverless installed locally to your project and not globally, to do this run npm install -D serverless or yarn add -D serverless

Here is my debug configuration

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Serverless Offline",
      "program": "${workspaceRoot}/node_modules/serverless/bin/serverless",
      "args": [
        "offline",
        "--noTimeout",
        "--dontPrintOutput",
        "--stage=local",
        "-P",
        "4000",
        "--aws-profile=#PROFILENAME#"
      ],
      "sourceMaps": true,
      "runtimeArgs": ["--lazy"],
      "outFiles": ["${workspaceFolder}/.webpack/**/*.js"],
      "protocol": "inspector",
      "runtimeExecutable": "node",
      "env": {
        // Here we set some environment vars that should be set locally.
        // They can and will overwrite the ones coming from your serverless.yml
      },
      "windows": {
        "program": "${workspaceRoot}\\node_modules\\serverless\\bin\\serverless"
      }
    }
  ]
}

In the example above, I included the "--aws-profile=#PROFILENAME#" argument, This is so that serverless knows which AWS profile I am using for deployment

Run your configured debug and test to see if everything runs as it should

Got an idea that's keeping you up at night? Let's chat!