From b8ccf8b83309a00907b709ccd40658b54cb95c69 Mon Sep 17 00:00:00 2001 From: Dominik Schubert Date: Wed, 2 Aug 2023 15:07:40 +0200 Subject: [PATCH 1/3] update JVM debugging docs --- .../tools/lambda-tools/debugging/index.md | 45 +++++++++++++------ 1 file changed, 32 insertions(+), 13 deletions(-) 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..5edec4d3b3 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 \ +--code file://handler.zip \ +--handler myindex.handler \ +--runtime nodejs16.x \ +--timeout 150 \ +--role arn:aws:iam::000000000000:role/lambda-role +--environment Variables='{"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,23 @@ 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. + +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 From 5b3efc51a000275eaa6834d0edc016217d25a373 Mon Sep 17 00:00:00 2001 From: Dominik Schubert Date: Wed, 2 Aug 2023 20:24:29 +0200 Subject: [PATCH 2/3] fix env variables in command sample --- content/en/user-guide/tools/lambda-tools/debugging/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 5edec4d3b3..0db631aaa3 100644 --- a/content/en/user-guide/tools/lambda-tools/debugging/index.md +++ b/content/en/user-guide/tools/lambda-tools/debugging/index.md @@ -214,7 +214,7 @@ $ awslocal lambda create-function --function-name debugfunc \ --runtime nodejs16.x \ --timeout 150 \ --role arn:aws:iam::000000000000:role/lambda-role ---environment Variables='{"Variables": {"_JAVA_OPTIONS": "-Xshare:off -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=0.0.0.0:5050"}}' +--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. From 501964e9d5cd356f7f1c500f26de26787078955a Mon Sep 17 00:00:00 2001 From: Dominik Schubert Date: Thu, 3 Aug 2023 10:30:25 +0200 Subject: [PATCH 3/3] pr issues --- .../en/user-guide/tools/lambda-tools/debugging/index.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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 0db631aaa3..6b19359220 100644 --- a/content/en/user-guide/tools/lambda-tools/debugging/index.md +++ b/content/en/user-guide/tools/lambda-tools/debugging/index.md @@ -209,11 +209,11 @@ When creating your Lambda function, set the `_JAVA_OPTIONS` environment variable {{< command >}} $ awslocal lambda create-function --function-name debugfunc \ ---code file://handler.zip \ +--zip-file fileb://java-handler.zip \ --handler myindex.handler \ ---runtime nodejs16.x \ +--runtime java8.al2 \ --timeout 150 \ ---role arn:aws:iam::000000000000:role/lambda-role +--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 >}} @@ -246,6 +246,7 @@ Now to debug your Lambda function, simply click on the `Debug` icon with `Remote 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`.