DEV Community

Vadym Kazulkin for AWS Heroes

Posted on • Edited on

Remote debugging for AWS Lambda Java runtime

Introduction

Following the announcement Simplify serverless development with console to IDE and remote debugging for AWS Lambda, I decided to try out the remote debugging for AWS Lambda with the Java runtime and write down my experiences with it.

Prerequisites

The following needs to be installed to use the remote debugging:

Remote Debugging

After starting Visual Studio Code, please select the AWS Toolkit extension. You'll see the Frankfurt, Europe AWS region selected by default in my case, and you can navigate and expand Lambda functions as displayed in the image below :

For demonstration purposes, we'll select GetProductByIdPureJava21Lambda function from our sample application and hit the "Invoke remotely" button as displayed in the image below:

Let's jump into the remote invoker configuration. First, we need to activate the "remote debugging" option. Then we need to configure the "local root path" to the Lambda Handler Java class. For our use case Lambda handler is configured in the SAM template as software.amazonaws.example.product.handler.GetProductByIdHandler::handleRequest. Therefore, we need to enter the local path "up to java package which the root package", like for my case, c:\source-code-java\AWSLambdaJavaSnapStart\pure-lambda-21\src\main\java

The last thing to configure is the sample event. As we use Amazon API Gateway as a proxy event, the sample event to invoke GetProductByIdPureJava21Lambda (and retrieve the product by ID equal to 1) looks like this:

{
    "httpMethod": "GET",
    "pathParameters": {
        "id": "1"
    }
}
Enter fullscreen mode Exit fullscreen mode

By configuring the "local root path" and activating the "remote debugging" option, the source code of the Lambda handler class software.amazonaws.example.product.handler.GetProductByIdHandler will open in the Visual Studio Code IDE, where we can set a breakpoint :

Then we need to hit the "Remote invoke" button in the "Remote invoke configuration" :

After waiting for some to Lambda function for setting up the debug session and being invoked, we see that the invocation was stopped at the breakpoint we set, and we can explore the values of the variables at the time of reaching this breakpoint and the "call stack" in the panels on the left side.

Below in the "output" section, we can also see the whole CloudWatch Logs output, which we can discover in the CloudWatch log group of this Lambda function:

We can also see there what is happening during the initialization phase of the Lambda function when using the remote debugger:

Please also take into account that in case Lambda SnapStart is activated for the Lambda function we intend to debug remotely, the procedure to establish the debugging session might take much longer.

You can explore in more depth how AWS Lambda remote debugging works under the hood in the article AWS Lambda remote debugging.

Conclusions

In this article, we tried out the remote debugging for AWS Lambda with the Java runtime and saw that it was relatively easy to set up and use. These added capabilities are very useful, for sure.

Top comments (0)