We have been developing AWS Lambda functions in Node JS for a few months. Can we debug, i.e. step through the Node JS code as we can with .Net C# code in Visual Studio?
Debugging lambda using breakpoints in VSCode:
First, write test cases for your handler as follows: index.test.js
'use strict';
const handler = require('../index');
const chai = require('chai');
const { expect } = chai;
describe('debug demo', () => {
it('should return success', () => {
let event = {data: "some data"}
handler(event, '', function(err, res) {
expect(res.statusCode, '200')
});
});
});
Now add the VSCode debugger configuration
Step 1: Click on debugger icon on the left side
Step 2: Click Add configurations
and add the following configurations in launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Mocha All",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999",
"--colors",
"'${workspaceFolder}/lambda-codebase/**/test/*.test.js'"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Mocha Current File",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999",
"--colors",
"${file}"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
Step 3: Now add a breakpoint to code which you wants to debug as follows:
Step 4: Now focus on the test file by clicking on the file:
Step 5: Choose the option from the drop-down "Mocha All" to run the complete solution and "Mocha current file" to run only selected file
Now click on the DEBUG Play button and enjoy the debugging!