Aws-sam-cli: Debugging SAM Local + Java in IntelliJ

Created on 12 Mar 2018  路  4Comments  路  Source: aws/aws-sam-cli

I am unable to hit breakpoints in IntelliJ (ver 14.1.7). I am using a Mac (ver. 10.13.3), SAM (ver. 0.2.8), and Docker (ver. 17.12.0-ce, build c97c6d6).

After starting sam local and then hitting the endpoint in Chrome, my console output is this:
```$ sam local start-api --debug-port 3000
2018/03/09 14:02:18 Connected to Docker 1.35
2018/03/09 14:02:18 Fetching lambci/lambda:java8 image for java8 runtime...
java8: Pulling from lambci/lambda
Digest: sha256:e1c82c7707c28f533480c76bb93a010f2279f699a0d7f8d3e9727082e0a81051
Status: Image is up to date for lambci/lambda:java8

Mounting com.amazonaws.lambda.hw.LambdaFunctionHandler::handleRequest (java8) at http://127.0.0.1:3000/ [GET]

You can now browse to the above endpoints to invoke your functions.
You do not need to restart/reload SAM CLI while working on your functions,
changes will be reflected instantly/automatically. You only need to restart
SAM CLI if you update your AWS SAM template.

2018/03/09 14:02:24 Invoking com.amazonaws.lambda.hw.LambdaFunctionHandler::handleRequest (java8)
2018/03/09 14:02:24 Decompressing /*/Hello World/target/hw-1.0.0.jar
2018/03/09 14:02:26 Mounting /private/var/folders/pr/zzhpj48x70l0gk6n1t11x74m0000gn/T/aws-sam-local-1520632944922829000 as /var/task:ro inside runtime container

It hangs after mounting and the page never loads.

My template.yaml looks like this:
```AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: 

Resources:
  GetFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: handleRequest
      Handler: com.amazonaws.lambda.hw.LambdaFunctionHandler::handleRequest
      Runtime: java8
      CodeUri: target/hw-1.0.0.jar
      Timeout: 300
      Events:
        GetEvent:
          Type: Api
          Properties:
            Path: /
            Method: get

My LambdaFunctionHandler.java looks like this:
```package com.amazonaws.lambda.hw;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import org.json.simple.JSONObject;

public class LambdaFunctionHandler implements RequestHandler {

@Override
public String handleRequest(Object input, Context context) {
    JSONObject obj = new JSONObject();
    obj.put("statusCode", 200);
    JSONObject headers = new JSONObject();
    headers.put("x-custom-header", "my custom header value");
    obj.put("headers", headers);
    obj.put("body", "Hello from Lambda!");

    return obj.toJSONString();
}

}
````

Remote Configuration Settings:
Transport: Socket
Debugger mode: Attach
Host: 127.0.01
Port: 3000

Invoking the function works fine with a default generated event file, but won't hit the breakpoints. Console output below:
```$ sam local invoke GetFunction --event evt.json
2018/03/12 10:15:57 Successfully parsed template.yml
2018/03/12 10:15:57 Connected to Docker 1.35
2018/03/12 10:15:57 Fetching lambci/lambda:java8 image for java8 runtime...
java8: Pulling from lambci/lambda
Digest: sha256:c2ec8db145bbe44154dbddcdd2976ab8afd60d58d7d633dc98f53f657c0ef909
Status: Image is up to date for lambci/lambda:java8
2018/03/12 10:15:59 Invoking com.amazonaws.lambda.hw.LambdaFunctionHandler::handleRequest (java8)
2018/03/12 10:15:59 Decompressing /*/Hello World/target/hw-1.0.0.jar
2018/03/12 10:16:00 Mounting /private/var/folders/pr/zzhpj48x70l0gk6n1t11x74m0000gn/T/aws-sam-local-1520874959484468000 as /var/task:ro inside runtime container
START RequestId: 28e2a514-e190-4bec-9681-fa194d91e654 Version: $LATEST
END RequestId: 28e2a514-e190-4bec-9681-fa194d91e654
REPORT RequestId: 28e2a514-e190-4bec-9681-fa194d91e654 Duration: 44.77 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 9 MB

"{\"headers\":{\"x-custom-header\":\"my custom header value\"},\"body\":\"Hello from Lambda!\",\"statusCode\":200}"
```

This is my first time trying to debug something remotely so I'm not sure what I'm doing wrong. Can anyone help? Thanks in advance.

Most helpful comment

It hangs after mounting and the page never loads.

This is the intended behaviour. The debugger is configured to break on the first line of code encountered, to give you a chance to attach your debugger.

Hit the URL in the browser, then attach your debugger of choice to the --debug-port specified.

All 4 comments

I'm not sure if it could be this simple, but make sure that you are passing the debug flag when you execute the sam commands:

$ sam local invoke -d 5858 <function logical id> 

Reference: https://github.com/awslabs/aws-sam-local#debugging-applications

It hangs after mounting and the page never loads.

This is the intended behaviour. The debugger is configured to break on the first line of code encountered, to give you a chance to attach your debugger.

Hit the URL in the browser, then attach your debugger of choice to the --debug-port specified.

For anyone finding this issue at a later date, I'd like to recommend this online video to help demonstrate the intended behavior @PaulMaddox describes.

I created a "remote debug" configuration in IntelliJ that points to port 5000.

I then ran sam local start-lambda -d 5000, and then start clicking on the "debug" button in IntelliJ until the docker container is up.

Was this page helpful?
0 / 5 - 0 ratings