[英]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.我对 AWS 和无服务器等完全陌生。为了加快开发速度,我希望能够在本地调试我的应用程序。
Following this article Running and Debugging AWS Lambda functions locally I have attempted to achieve just that.在本文在本地运行和调试 AWS Lambda 函数之后,我试图实现这一目标。
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).在 Visual Studio Code 中,当我运行调试配置时,应用程序会立即退出而不会出错(在“内容”变量的声明和初始化上设置了断点)。 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:
我试图在“index.js”中定义的主“处理程序”函数中输入:
exports.handler = (event, context, callBack) =>
{
let bIsPostRequest = false, bIsPutRequest = false, bIsGetRequest = false, bIsDelRequest = false;
let content = "";
...
Here is my 'launch.json' configuration file:这是我的“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"
]
}
]
}
Also, I am not 100% certain on the definition of 'outfiles' in the configuration.另外,我不是 100% 确定配置中“输出文件”的定义。 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.
我得出的结论是它是我试图调试的文件,但是如果是这种情况,“outfiles”对我来说似乎不是一个合适的名称。
The local environment I am working in is a windows one.我工作的本地环境是 windows 环境。
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.我在主工作目录中将此文件保存为“debugLocal.js”。 The launch.json file now looks as follows:
launch.json 文件现在如下所示:
{
"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 ie
require("./js/inc/globalDefines.js");
需要注意的一件事是包含的文件路径必须稍作更改,即
require("./js/inc/globalDefines.js");
instead of require("js/inc/globalDefines.js");
而不是
require("js/inc/globalDefines.js");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.