How to debug aws lambda functions written in node js

后端 未结 9 3304
孤城傲影
孤城傲影 2021-02-20 13:34

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?

相关标签:
9条回答
  • As others have pointed out, you can't natively step debug a Lambda. But new tools are being developed that make it possible. Rookout now offers step debugging of production Node.js Lambdas without forking or stopping the code, using some sort of bytecode-level method.

    0 讨论(0)
  • 2021-02-20 13:57

    Debugging lambda using breakpoints in VSCode:

    First, write test cases for your handler as follows: index.test.js

    'use strict';
    
    const handler = require('../index');
    
    const chai = require('chai');
    
    const { expect } = chai;
    
    describe('debug demo', () => {
      it('should return success', () => {
        let event = {data: "some data"}
        handler(event, '', function(err, res) {
          expect(res.statusCode, '200')
        });
      });
    });
    

    Now add the VSCode debugger configuration

    Step 1: Click on debugger icon on the left side

    Step 2: Click Add configurations

    and add the following configurations in launch.json file:

    {
        "version": "0.2.0",
        "configurations": [
    
          {
              "type": "node",
              "request": "launch",
              "name": "Mocha All",
              "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
              "args": [
                  "--timeout",
                  "999999",
                  "--colors",
                  "'${workspaceFolder}/lambda-codebase/**/test/*.test.js'"
              ],
              "console": "integratedTerminal",
              "internalConsoleOptions": "neverOpen"
          },
          {
              "type": "node",
              "request": "launch",
              "name": "Mocha Current File",
              "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
              "args": [
                  "--timeout",
                  "999999",
                  "--colors",
                  "${file}"
              ],
              "console": "integratedTerminal",
              "internalConsoleOptions": "neverOpen"
          }
        ]
      }
    

    Step 3: Now add a breakpoint to code which you wants to debug as follows:

    Step 4: Now focus on the test file by clicking on the file:

    Step 5: Choose the option from the drop-down "Mocha All" to run the complete solution and "Mocha current file" to run only selected file

    Now click on the DEBUG Play button and enjoy the debugging!

    0 讨论(0)
  • 2021-02-20 14:00

    If you are using serverless and serverless offline plugin to test in local. You can refer below:

    If you are using windows, update the vscode launch.json and package.json as below :

    // launch.json
    {
    
        "version": "0.2.0",
    
       "configurations": [
    
           {
    
               "type": "node",
    
               "request": "launch",
    
               "name": "Debug Serverless",
    
               "cwd": "${workspaceFolder}",
    
               "runtimeExecutable": "npm",
    
               "runtimeArgs": [
    
                   "run",
    
                   "debug"
    
               ],
    
               "outFiles": [
    
                   "${workspaceFolder}/handler.js"
    
               ],
    
               "port": 9229,
    
               "sourceMaps": true
    
           }
    
       ]
    
    }

    // package.json
    ....
    "scripts": {
        "debug": "SET SLS_DEBUG=* && node --inspect %USERPROFILE%\\AppData\\Roaming\\npm\\node_modules\\serverless\\bin\\serverless offline -s dev"
      }

    If on linux your debug script will be:

    // package.json
    ....
    "scripts": {
        "debug": "export SLS_DEBUG=* && node --inspect /usr/local/bin/serverless offline -s dev"
      }
    
    0 讨论(0)
  • 2021-02-20 14:09

    The easiest option I have so far with VS Code is this:

    1. Create a new file, lets say call it debug.js and just call your lambda function from here, something like this: const app = require('./index') app.handler()

    2. Change program entry of the launcher.json file like this: "program": "${workspaceFolder}/<your app dir>/debug.js"

    3. You can now just put a breakpoint on this line (app.handler()) and it works

    0 讨论(0)
  • 2021-02-20 14:10

    I would like to share what I have found as I had had some hard time trying to find it out. The solution is based on what I found on article "Debugging AWS Lambda Functions Locally using VS Code and lambda-local"(https://www.codeproject.com/Articles/1163890/Debugging-AWS-Lambda-functions-locally-using-VS-Co) with some modification in order to work in our Windows based environment.

    Here is the summary:

    1) To use Visual Studio Code to lunch a debug session. Am example of launch.json for debugging 'llDebugDetail.js' is as below:

    {
        "version": "0.2.0",
        "configurations": [
            {
                "type": "node",
                "request": "launch",
                "name": "Launch Program",
                "program": "${workspaceRoot}/test/llDebugDetail.js",
                "cwd": "${workspaceRoot}"
            }
        ]
    }
    

    2) To use the lambda-local framework to call (execute) the lambda function. The lambda-local framework has to be installed locally, otherwise the VSC debugger won't find it. Our lambda function is invoked by calling the following URL: https://xxx.execute-api.ap-southeast-2.amazonaws.com/resourceName/{id}/detail. The {id} is a GUID parameter for selecting a product item and returning its details.

    Here is the 'llDebugDetail.js' code for calling the 'getDetail' function for returning product details with the GUID as its id in the URL. The function is within the 'getDetail.js' file.

    const lambdaLocal = require('lambda-local');
    var path = require("path");
    const myPath = path.join(__dirname, '../getDetail.js');
    const testEvent = {
        "resource": "resourceName/12da3f7d-7ce2-433f-8659-5d7cd0d74d9a/detail",
        "pathParameters": {"id": "12da3f7d-7ce2-433f-8659-5d7cd0d74d9a"}
    }
    
    var lambdaFunc = require(myPath);
    
    lambdaLocal.execute({
        event: testEvent,
        lambdaFunc: lambdaFunc, 
        lambdaHandler: "getDetail"
    }).then(function(done) {
        console.log(done);
    }).catch(function(err) {
        console.log(err);
    });
    

    With the above done, you can now set break points anywhere within your code, e.g. inside getDetail.js, launch the program and the step through the code from the break points within getDetail.js.

    0 讨论(0)
  • 2021-02-20 14:12

    You cannot debug the lambda code like you do in VS but you can invoke some test data and check everything is fine.

    1. You can run lambda locally in your machine using lambda-local and serverless-offline
    2. So invoke your lambda with some test event & data and you can log and see whats happening for different inputs
    0 讨论(0)
提交回复
热议问题