Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update JVM debugging docs #744

Merged
merged 3 commits into from
Aug 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions content/en/user-guide/tools/lambda-tools/debugging/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,9 @@ $ awslocal lambda invoke --function-name my-cool-local-function --payload '{"mes

## Debugging JVM lambdas

### Configure LocalStack for remote JVM debugging
### Configure LocalStack and your Lambda function for remote JVM debugging

Set `LAMBDA_JAVA_OPTS` with `jdwp` settings and expose the debug port
(you can use any other port of your choice):
Set `LAMBDA_DOCKER_FLAGS` to export the `5050` (you can use any other port of your choice) port which your IDE debugger will connect to.

```yaml
#docker-compose.yml
Expand All @@ -203,12 +202,26 @@ services:
...
environment:
...
- LAMBDA_JAVA_OPTS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5050
- LAMBDA_DOCKER_FLAGS=-p 127.0.0.1:5050:5050
```

Note the `suspend=y` option here, it will delay code execution until the debugger is
attached to the debugger server. If you want to change that, simply switch to `suspend=n`.
When creating your Lambda function, set the `_JAVA_OPTIONS` environment variable like so:

{{< command >}}
$ awslocal lambda create-function --function-name debugfunc \
--zip-file fileb://java-handler.zip \
--handler myindex.handler \
--runtime java8.al2 \
--timeout 150 \
--role arn:aws:iam::000000000000:role/lambda-role \
--environment '{"Variables": {"_JAVA_OPTIONS": "-Xshare:off -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=0.0.0.0:5050"}}'
{{< /command >}}

Note the `suspend=y` option here, it will delay code execution until the debugger is attached to the debugger server.
If you want to change that, simply switch to `suspend=n`.

By default the runtime environment for Java will set `-Xshare: on`, so we'll have to disable it here again.
Your IDE might show you the listen address as `*:5050`, but please note that this only works for Java 9+.

### Configuring IntelliJ IDEA for remote JVM debugging

Expand All @@ -221,17 +234,24 @@ while [[ -z $(docker ps | grep :5050) ]]; do sleep 1; done

![Run/Debug Configurations](inteliji-debugging-jvm-1.png)

This shell script should simplify the process a bit since the debugger server is not
immediately available (only once lambda container is up).
This shell script should simplify the process a bit since the debugger server is not immediately available (only once Lambda container is up).

Then create a new `Remote JVM Debug` configuration and use the script from
above as a `Before launch` target:
Then create a new `Remote JVM Debug` configuration and use the script from above as a `Before launch` target:

![Run/Debug Configurations](inteliji-debugging-jvm-2.png)

Now to debug your lambda function, simply click on the `Debug` icon with
`Remote JVM on LS Debug` configuration selected, and then invoke your
lambda function.
Now to debug your Lambda function, simply click on the `Debug` icon with `Remote JVM on LS Debug` configuration selected, and then invoke your Lambda function.

### Alternative setup for IntelliJ IDEA

The debugger can also act as a server by changing the drop-down "Debugger mode" to "Listen to remote JVM".
dominikschubert marked this conversation as resolved.
Show resolved Hide resolved
In this case you should not set `LAMBDA_DOCKER_FLAGS` since the port will be exposed on your host instead of the Lambda container.
Compared to the previous setup the "Wait Remote Debugger Server" run configuration should also be removed and instead tick the mark at "Auto restart" after switching to the "Listen to remote JVM" mode.

For the Lambda function you will have to adjust the environment variable to `"_JAVA_OPTIONS": "-Xshare:off -agentlib:jdwp=transport=dt_socket,server=n,address=172.17.0.1:5050,suspend=y,onuncaught=n"`.
Notice the `address=172.17.0.1:5050`.
Here we tell the Lambda function to connect to port 5050 on 172.17.0.1. When using Docker desktop you might have to set this to `address=host.docker.internal:5050` instead.


### Configuring Visual Studio Code for remote JVM debugging

Expand Down
Loading