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 the Visual Studio Code, please select AWS Toolkit extension. You'll see Europe Frankfurt AWS region select by default in my case and you can navigate and expand Lambda functions as displayed in the image below :

For the 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 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 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 proxy event, the sample event to invoke GetProductByIdPureJava21Lambda (and retrieve the product by ID equals to 1) looks like this:

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

By configuring the "local root path" and activating "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 "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 break point 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 whole CloudWatch Logs output which we can see 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 the account, that in case Lambda SnapStart is activated for the Lambda function we intent 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)