Debugging lambda functions locally in vscode with actual break-points

David Borgenvik
3 min readMay 8, 2017

--

Instead of telling you how long I’ve been working with serverless and how awesome I’am. I’d thought let’s just get to the point. First things first if you are not in a reading mode go to this git-repo and clone it and run it it will probably give you more then the text below ;)

https://github.com/OneMuppet/local-lambda-debug

Who should read this?

  • You have AWS Lambda functions in your stack
  • You use vscode as your IDE
  • You use Serverless (the framework)
  • You use Serverless Offline (the plugin to the framework)
  • You are frustrated because you think your dev-cycle is too long and you wish you could just set a break point in a lambda function and debug it.

Setup

package.json, below is the script section of the package.json file:

"scripts": {
"start": "./node_modules/.bin/serverless offline -s dev",
"debug": "export SLS_DEBUG=* && node --debug ./node_modules/.bin/serverless offline -s dev",
"test": "mocha"
}

It’s the debug section that will focus on here:

export SLS_DEBUG=*
Means that the Serverless Framework and it’s plugin will be verbose in the logging, which is always good when debugging.
node --debug ./node_modules/.bin/serverless offline -s dev
This tells node to run in debug mode and execute the serverless binary in the node_modules directory and run the plugin called offline with the stage dev.

That’s it for the package.json file. Now we need to tell vscode to run this code and attach a debugger to it whenever we press F5.

Click on the debug symbol and then on the cog to setup a launch configuration. You chose:

Then copy and paste this configuration into you launch.json file:

{
"version": "0.2.0",
"configurations": [
{
"name": "Debug",
"type": "node",
"request": "launch",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
"debug"
],
"port": 5858
}
]
}

This will create a launch configuration called Debug, that will run your debug task that you have setup in you package.json.

Start debugging

Now you simply hit F5 or click debug > chose Debug and click play, your lambda functions will run locally with a debugger attached. This is what it would look like:

vs code running Serverless framework and serverless offline to debug local lambda functions with break points

What I’ve done above is simply, hit F5 and then opened Postman and posted a json-object to my local lambda function.

— — — — — — — — — — — — — — — — — — — — — — — — — — - — — -

Update: I really recommend looking at Chris Rogers response below for how to set this up on windows. Big thanks for taking the time to share your setup!

--

--

David Borgenvik

I'm the founder of TIQQE - a software company focusing on Serverless development on AWS