简体   繁体   English

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

[英]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.

相关问题 如何使用无服务器框架通过 AWS API 网关在 Node.js 中编写的 AWS Lambda function 上返回错误? - How do I return errors on an AWS Lambda function written in Node.js through the AWS API Gateway using the Serverless framework? 无法使用无服务器框架在AWS Lambda中部署Node.js应用程序 - Cannot deploy Node.js app inside AWS Lambda using Serverless Framework 是否有关于如何通过 Lambda 连接到 AWS Aurora Serverless PostgreSQL 的 Node.js 示例 - Are there Node.js examples for how to connect to AWS Aurora Serverless PostgreSQL via Lambda 如何使用 node.js 使用 aws Lambda 发布文件 - How to post file using aws Lambda using node.js 如何在 lambda 中使用 node.js 实现 aws 视频转码器 - How to implement aws video transcoder using node.js in lambda 在异步 AWS Lambda 函数中使用带有 node-fetch 模块的 node.js 时遇到问题 - trouble using node.js w/ node-fetch module in async AWS Lambda Function AWS Lambda node.js 'ping' 模块权限错误 - AWS Lambda node.js 'ping' module permission error AWS 无服务器图像处理程序 - 版本 5 - Lambda 在 Node.js 上运行 12 生命周期结束 - AWS Serverless Image Handler - Version 5 - Lambda running on Node.js 12 End of Life 如何从 AWS Lambda (Node.js) 中的处理程序调用 module.exports - How to call module.exports from handler in AWS Lambda (Node.js) 无法在本地调试node.js - Can not debug node.js locally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM