Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Java: Remote Attach",
"type": "java",
"request": "attach",
"hostName": "localhost",
"preLaunchTask": "Wait Remote Debugger Server",
"port": 5050
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Wait Remote Debugger Server",
"type": "shell",
"command": "while [[ -z $(docker ps | grep :5050) ]]; do sleep 1; done; sleep 1;"
}
]
}
48 changes: 48 additions & 0 deletions lambda-debug-mode/java/base-enable-lambda-debug-mode/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export AWS_ACCESS_KEY_ID ?= test
export AWS_SECRET_ACCESS_KEY ?= test
export AWS_DEFAULT_REGION = us-east-1
VENV_DIR ?= .venv

# https://hub.docker.com/_/gradle/tags
IMAGE ?= gradle:8.4.0-jdk17

usage: ## Show this help
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'

install: build-docker

build-docker: ## Build the Lambda function zip in Docker
docker run --rm -v "$$(pwd)/java-function:/app" $(IMAGE) bash -c "cd /app && ./gradlew buildZip"

build-local: ## Build the Lambda function zip locally
cd java-function && ./gradlew buildZip

clean:
rm -rf java-function/build

run: ## Deploy and invoke the Lambda container locally
echo "Deploying Lambda locally"; \
./run.sh; \
echo "Done - test successfully finished."

start:
LOCALSTACK_LAMBDA_DEBUG_MODE=1 \
LOCALSTACK_LAMBDA_DEBUG_MODE_CONFIG_PATH=/tmp/lambda_debug_mode_config.yaml \
localstack start --volume ${PWD}/lambda_debug_mode_config.yaml:/tmp/lambda_debug_mode_config.yaml -d

stop:
@echo
localstack stop

ready:
@echo Waiting on the LocalStack container...
@localstack wait -t 30 && echo Localstack is ready to use! || (echo Gave up waiting on LocalStack, exiting. && exit 1)

logs:
@localstack logs > logs.txt

test-ci:
make install; return_code=`echo $$?`;\
echo "Interactive debugging not tested in CI"; exit $$return_code;

.PHONY: usage install run start stop ready logs test-ci build-local build-docker clean
54 changes: 54 additions & 0 deletions lambda-debug-mode/java/base-enable-lambda-debug-mode/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# LocalStack Demo: Enable Lambda Debug Mode to Automatically Raise Execution Timeouts

A simple demo application showcasing how to debug Java Lambdas locally with Lambda Debug Mode.
The demo deploys a Lambda function with a one-second timeout, which is automatically lifted when running LocalStack with Lambda Debug Mode enabled.

## Prerequisites

* LocalStack
* Docker
* `make`
* [`awslocal`](https://github.com/localstack/awscli-local)
* `java17` and `gradle` (optional for local build)

## Installing

To build the Lambda function archive:

```sh
make install
```

## Starting Up

Make sure that LocalStack is started with the following configuration:

```sh
LOCALSTACK_LAMBDA_DEBUG_MODE=1 \
LOCALSTACK_LAMBDA_DEBUG_MODE_CONFIG_PATH=/tmp/lambda_debug_mode_config.yaml \
localstack start --volume $PWD/lambda_debug_mode_config.yaml:/tmp/lambda_debug_mode_config.yaml
```

* `LOCALSTACK_LAMBDA_DEBUG_MODE=1` enables the Lambda debug mode
* `LOCALSTACK_LAMBDA_DEBUG_MODE_CONFIG_PATH=/tmp/lambda_debug_mode_config.yaml` points to the config file for Lambda debug mode allowing for advanced configuration. It maps the Lambda function `arn:aws:lambda:us-east-1:000000000000:function:function-one` to port `5050`.
* `--volume $PWD/lambda_debug_mode_config.yaml:/tmp/lambda_debug_mode_config.yaml` maps the Lambda debug configuration from the host into the LocalStack Docker container for hot-reloading.

## Running the Sample

The project requires you to configure your IDE or editor of choice to debug remote Java Lambda functions on port 5050.
[These documentations](https://docs.localstack.cloud/user-guide/lambda-tools/debugging/#debugging-jvm-lambdas) include a guide on how you can do so.

The following command used to deploy and invoke the Lambda locally:

```sh
make run
```

### Attaching the Remote Debugger

After the Lambda function is invoked you can switch to your IDE or editor of choice, set a breakpoint in the Lambda handler, and run the remote debugger.
LocalStack will automatically waive the set one second timeout for the Lambda function, giving you ample time to connect the debugger and debug the logic in the function.

## License

The code in this sample is available under the Apache 2.0 license.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
plugins {
id 'java'
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}


repositories {
mavenCentral()
}

dependencies {
implementation 'com.amazonaws:aws-lambda-java-core:1.2.2'
implementation 'com.amazonaws:aws-lambda-java-events:3.11.1'
runtimeOnly 'com.amazonaws:aws-lambda-java-log4j2:1.5.1'
}

tasks.register('buildZip', Zip) {
into('lib') {
from(jar)
from(configurations.runtimeClasspath)
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading
Loading