MikeO
MikeO

Reputation: 493

How to debug AWS Lambda Node.js locally using serverless module?

I am completely new to AWS and serverless etc. To speed up development I would like the ability to debug my application locally.

Following this article Running and Debugging AWS Lambda functions locally I have attempted to achieve just that.

In Visual Studio Code when I run the debug configuration, the application exits instantly without error (A break-point is set on the declaration and initialisation of the 'content' variable). I am not sure I have the function name correct. I am trying to enter at the main 'handler' function defined in 'index.js' as:

exports.handler = (event, context, callBack) =>
{   
    let bIsPostRequest = false, bIsPutRequest = false, bIsGetRequest = false, bIsDelRequest = false;
    let content = "";

...

Here is my 'launch.json' configuration file:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Debugger",
            "program": 
             "${workspaceFolder}\\node_modules\\serverless\\bin\\serverless",
            "args":[
                "invoke",
                "local",
                "-f",
                "index.handler", // function name
                "--data",
                "{}"
            ],
            "outFiles": [
                "${workspaceFolder}\\index.js"
            ]
        }
    ]
}

Also, I am not 100% certain on the definition of 'outfiles' in the configuration. I have come to the conclusion it is the file(s) I am trying to debug, however if this is the case 'outfiles' does not seem a fitting name to me.

The local environment I am working in is a windows one.

Upvotes: 1

Views: 2543

Answers (1)

MikeO
MikeO

Reputation: 493

After coming across this post I managed to get the debugger working. Here is the configuration to match my needs:

const lambdaLocal = require('lambda-local');
var lambdaFunc = require("./index.js");

lambdaLocal.execute({
lambdaFunc: lambdaFunc, 
lambdaHandler: "handler",
event: {
    context: {
        "resource-path": "/products",
        "http-method": "GET"
    },
    "body-json": {
        name : "ProductA"
    }
 }
}).then(function(done) {
    console.log(done);
}).catch(function(err) {
    console.log(err);
});

I saved this file as 'debugLocal.js' in my main working directory. The launch.json file now looks as follows:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Debugger",
            "program": "${workspaceFolder}\\debugLocal.js"
        }
    ]
}

So far everything appears to be replicated fairly well. One thing to note is the file paths on includes had to be changed slightly i.e. require("./js/inc/globalDefines.js"); instead of require("js/inc/globalDefines.js");

Upvotes: 2

Related Questions