如何使用无服务器模块在本地调试 AWS Lambda Node.js?

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

我对 AWS 和无服务器等完全陌生。为了加快开发速度,我希望能够在本地调试我的应用程序。

在这篇文章之后 Running and Debugging AWS Lambda functions locally 我已经尝试做到这一点。

在Visual Studio代码中,当我运行调试配置时,应用程序立即退出而没有错误(在'content'变量的声明和初始化时设置了断点) .我不确定函数名称是否正确。我正在尝试在 'index.js' 中定义的主要 'handler' 函数中输入:

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

...

这是我的 'launch.json' 配置文件:

{
    "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"
            ]
        }
    ]
}

此外,我不是 100% 确定 'outfiles' 在配置中的定义。我得出的结论是我正在尝试调试的文件,但是如果是这种情况,'outfiles' 对我来说似乎不是一个合适的名称。

我工作的当地环境是windows。

遇到 this post 后,我设法使调试器工作。这是符合我需要的配置:

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);
});

我将此文件另存为 'debugLocal.js' 在我的主工作目录中。 launch.json 文件现在如下所示:

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

到目前为止,一切似乎都被很好地复制了。需要注意的一件事是必须稍微更改包含的文件路径,即 require("./js/inc/globalDefines.js"); 而不是 require("js/inc/globalDefines.js");