diff --git a/content/en/user-guide/tools/lambda-tools/debugging/index.md b/content/en/user-guide/tools/lambda-tools/debugging/index.md index ed79aafbda..6b19359220 100644 --- a/content/en/user-guide/tools/lambda-tools/debugging/index.md +++ b/content/en/user-guide/tools/lambda-tools/debugging/index.md @@ -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 @@ -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 @@ -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". +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