如何调试用Node JS编写的AWS Lambda函数 [英] How to debug aws lambda functions written in node js

查看:250
本文介绍了如何调试用Node JS编写的AWS Lambda函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个月来,我们一直在Node JS中开发AWS Lambda函数.我们可以像在Visual Studio中使用.Net C#代码那样调试,即逐步完成Node JS代码吗?

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?

推荐答案

许多Lambda函数默认都不使用基于IDE的开发工具.有一些插件,例如AWS在其博客 https://aws.amazon.com/blogs/developer/aws-lambda-support-in-visual-studio/,但是这些功能和支持的水平各不相同.

IDE-based development tools are not available by default for many Lambda functions. There are some plugins, such as the Visual Studio support introduced by AWS on their blog at https://aws.amazon.com/blogs/developer/aws-lambda-support-in-visual-studio/, but these will have varying levels of feature sets and support.

为了使用逐步调试来测试Lambda,您需要专注于两个领域-运行Lambda的硬件以及调用Lambda函数的方式.由于AWS会将运行lambda函数的机器实例的详细信息保密,因此硬件在仿真方面具有挑战性.因此,在仿真硬件时,您只需要保持适用于您的语言和操作系统的合理范围-确保安装了正确的运行时(例如,在安装时不要安装NodeJS4.X. (使用版本6运行时),请确保您没有超出存储要求(用于Lambda的AMI在/tmp中获得500 MB的临时存储空间),并确保在运行之前未在本地保存任何状态代码.

In order to test Lambda using step debugging, you'll need to focus on two domains - the hardware on which it runs, and the way in which your Lambda function is invoked. The hardware is challenging to emulate, as AWS keeps the particulars of the machine instances that run your lambda functions secret. As such, when it comes to emulating hardware, you'll simply need to keep within what's reasonable for your language and operating system - make sure that the correct run time is installed (as in, don't install NodeJS 4.X when you're working with the version 6 runtime), make sure you don't exceed storage requirements (AMIs for Lambda get 500 MB of temporary storage space in /tmp), and ensure you're not saving any state locally prior to runs of your code.

一旦您确定了机器要求(或者由于代码不执行任何特定于硬件的工作而决定通过这些要求),那么您将需要编写一个测试工具来调用您的AWS Lambda函数.此测试工具充当调试器的入口点,尽管就AWS调用Lambda的方式而言,它很有可能不是100%准确(例如,context参数包含有关当前Lambda调用的信息,这自然而然地)每次执行之间会有所不同),这使您可以调用所有标准编码支持工具.

Once you've nailed down your machine requirements (or decided to pass on them as your code doesn't do any hardware-specific work), then you'll need to write a test harness to invoke your AWS Lambda function. This test harness serves as an entry point for your debugger, and while it is most likely not 100% accurate with respect to how AWS invokes Lambda (for example, the context parameter contains information on your current Lambda invocation, which will by nature vary between executions), it gets you to the point where you can invoke all of your standard coding support tools.

注意:以下简单的测试工具是为Node.JS编写的,但是您可以将概念适应Lambda执行的运行时

Note: the following simple test harness is written for Node.JS, but you can adapt the concepts to the runtime in which your Lambda executes

我们要做的第一件事是创建一个新文件debug.js并导入处理程序函数原型.假设您已经在handler.js中定义了处理程序,并将其命名为handler,则可以执行以下操作:

The first thing we'll do is create a new file - debug.js - and import the handler function prototype. Assuming you've defined your handler in handler.js, and called it handler, you do that as follows:

var handler = require('./handler.js').handler;

var handler = require('./handler.js').handler;

接下来,我们需要调用处理程序函数.正如我在上文中提到的,每个参数都有不同的用途.处理程序的第一个参数-event-具有导致调用的事件的详细信息. 注意:这还包括您的函数参数.正如我们所讨论的,第二个参数包含有关运行函数的上下文的信息.还有第三个参数,回调,可用于在Lambda执行完成时调用回调.在此处查看AWS文档: http://docs .aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

Next, we need to invoke the handler function. As I alluded to above, each of the parameters has a different purpose. The first parameter to the handler -event - has details of the event causing the invocation. Note: This also includes your function arguments. The second parameter, as we discussed, contains information on the context in which your function is running. There's also a third parameter, callback, that can be used to invoke a callback upon completion of your Lambda execution. Review the AWS docs here: http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

因此,出于我们的目的,对于简单的测试工具,我们只需要通过event参数发送参数即可.我们暂时将contextcallback参数保留下来(进行较小的修改,在下面进行更多说明),但是如果您想在那里提供函数依赖的额外数据就可以了-只要确保它没有不会与放置在AWS中的任何自动化数据冲突.因此,我们使用debug.js中的以下代码定义参数哈希,并调用该函数:

So for our purposes, for a simple test harness, we just need to send the parameters through in the event parameter. We'll leave the context and callback parameters alone for now (with a minor modification, more on that below), but if you want to provide extra data there that your function relies upon that's fine - just make sure it doesn't conflict with any of the automated data put there in AWS. So we define the parameter hash, and invoke the function, using the following code in debug.js:

var parameters = {
    "key1":"val1",
    "object" :{},
    // other keys as necessary
};

handler(parameters, {succeed:function(result){
    console.log("success: ", JSON.stringify(result, null, 2));
    process.exit(0);
}, fail:function(error){
    console.error("error: ", error);
    process.exit(1);
}});

这段代码做了一些有趣的事情:

This code does a few interesting things:

  • 它使用成功和失败处理程序使上下文对象过载.您可以将它们包装在"if"语句中,然后使用context.succeed(message)context.fail(error)在Lambda代码中调用它们. Lambda并没有正式支持这些功能,而是在我们的代码中将其用作解决方法,以获取对成功/失败行为的访问权限
    • 处理程序使用适当的错误代码调用process.exit().这使您可以将执行链接到CI/CD工具或使用流程退出代码作为控制流的任何其他批处理工具中
    • It overloads the context object with a success and fail handler. You can wrap these in an "if" statement, and invoke them in your Lambda code using context.succeed(message) or context.fail(error). These are not officially supported by Lambda, but are instead used as a workaround in our code to get access to success/fail behavior
      • Handlers call process.exit() with the appropriate error code. This allows you to chain your execution into CI/CD tools, or any other batch processing tools that use process exit code as control flow

      一旦您编写了此简单的测试工具,并修改了Lambda代码以调用成功/失败处理程序(如果存在的话,则简单到if(context.success){context.success(args);}就足够了),现在您可以使用以下命令来调用lambda函数node debug.js,然后在控制台中查看结果.

      Once you've written this simple test harness, and adapted your Lambda code to invoke the success/fail handler if they are present (something as simple as if(context.success){context.success(args);} should be sufficient), you can now invoke the lambda function using node debug.js and see the results in the console.

      我在Lambda函数中进行单元测试也很幸运.现在您有了一个入口点,以及一个有关如何调用Lambda函数的示例,您应该能够编写表达所有功能的合适的单元和功能测试.

      I've also had great luck with unit testing in my Lambda functions. As you now have an entry point, and an example of how to call the Lambda function, you should be able to write suitable unit and function tests that express all of your functionality.

      正如我提到的,这种方法并不完美.以下是测试工具可能出现的一些问题:

      As I mentioned, this approach isn't perfect. Here are a few problems with the test harness that could potentially arise:

      • 我们尚未对上下文对象进行任何仿真.您可以在 http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html -您需要进行反复试验才能弄清楚到底是哪种格式在这些参数中
      • 我们没有做任何机器仿真来发现硬件问题
      • 我们在这里仅介绍了Node.js函数,其他语言可能无法适应回调方法
      • 我们已经重载了上下文机制来提供成功/失败处理程序.如果AWS将名称相似的成员对象添加到context对象,则此方法可能会遇到问题
      • We haven't done any emulation of the context object. You can see the parameters available in the context object at http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html - you'll need to do some trial and error to figure out exactly what format ends up in these parameters
      • We have not done any machine emulation, to catch hardware issues
      • We've only covered Node.js functions here, other languages may have trouble adapting the callback approach
      • We've overloaded the context mechanism to provide our success-fail handlers. If AWS adds similarly-named member objects to the context object, this approach may run into problems

      尽管如此,您现在应该可以使用本地调试工具来测试和调试Lambda函数.我们在Backand上使用了类似的框架- https://www.backand.com -我们的Lambda函数开发工具,大大提高了我们Lambda的开发速度.

      However, despite the above, you should now have the capability to use local debugging tools to test and debug your Lambda functions. We use a similar framework at Backand - https://www.backand.com - for our Lambda function development tool, and it has greatly increased our Lambda development velocity.

      这篇关于如何调试用Node JS编写的AWS Lambda函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆