How to Debug AWS Lambda?

One problem that was often seen as the inability to run and debug functionalities locally. The Visual Studio (VS) Code and the Serverless framework are required for debugging the AWS Lambda using Node.js. There is an SDK available for Python and Java as well.

The Serverless framework has a feature to invoke the local command. This emulates the AWS Lambda environment though maybe there could be some minor errors which will not have much impact.

serverless invoke local –function functionName

Use the VS Code to debug Node.js. The Visual Studio Code has an inbuilt support feature that can debug the Node.js runtime and debug JavaScript, TypeScript and many other languages.

Two wire protocols exist:

  1. legacy: the original V8 Debugger Protocol which is currently supported by older runtimes.
  2. inspector: the new V8 Inspector Protocol is exposed via the –inspect flag in Node.js versions >= 6.3. It addresses most of the limitations and scalability issues of the legacy protocol.

You now can locally run and debug your Lambda functions.

Step 1:

Deploy the Serverless framework as devDependency

There are many advantages of using the Serverless framework as a dependency in a project since

  1. It enables the developers to integrate their work together with the CI (continuous integration) to avail the Serverless Framework without the need of installing in their systems.
  2. It will allow seamless operation even if a non-compatible version of the Serverless Framework Is deployed. Typically, the version to be used must match the one used by the serverless.yml file in the project
  3. The file size installed will not make an impact since the Serverless v1.16.0 devDependencies are eliminated from the deployment package,

Step 2:

Enable the debug configuration

Invoke the “sls invoke local” CLI command serverless invoke local –function functionName

Or the local function invocation can be done with custom context.

serverless invoke local –function functionName –context “hello world”

You may also invoke the local function with data passing like in a JSON file.

serverless invoke local –function functionName –contextPath lib/context.json

Step 3:

Start debugging

Click on the green triangle icon in the Debug view on the VS Code. This may also be done using your keyboard functions of Ctrl + Shift+ D. Debug View will show all the relevant information about debugging with a set of commands and configuration settings on the top of the screen.

The debugging configuration information in a launch.json file is stored by the VS Code in a .vscode folder in the project root folder or in the user settings. To start the debugging session, select Launch Program using the configuration drop-down menu or you could also use Command Palette using the filter on Debug (Select start debugging). The Debug Console status bar will turn Orange.

The toolbar will show at the top of the editor will display signs like Continue/Pause, Step-over, Step-into, Step-Out, Restart or Stop etc. for you to drag the function you want. The session will stop automatically once the debug program is over.

Avail the free AWS Cloud demo before your final purchase. Call our experts 1800-212-2022/1888-288-3570.

Have questions?

Ask us.



    AWS Standard Consulting Partner

    • Go4hosting
    • Go4hosting

    Alibaba Cloud

    Go4hosting

    Go4hosting-NOW-NASSCOM-Member Drupal Reseller Hosting Partner

    Cyfuture Ltd.

    The Cricket Barn
    Tiverton
    Exeter
    EX16 8ND

    Ph:   1-888-795-2770
    E-mail:   [email protected]