Skip to content

Commit 3324d1f

Browse files
MEPalmajoe4dev
andauthored
[LambdaDevX] Add Base Demo Projects for Lambda Debug Mode (#253)
* python samples * java sample * concurrency sample * imp delay params and timeouts * Add volume mounts and improve sample scripts * Add Docker build for Java sample --------- Co-authored-by: Joel Scheuner <[email protected]>
1 parent de13f51 commit 3324d1f

File tree

33 files changed

+1271
-0
lines changed

33 files changed

+1271
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Java: Remote Attach",
6+
"type": "java",
7+
"request": "attach",
8+
"hostName": "localhost",
9+
"preLaunchTask": "Wait Remote Debugger Server",
10+
"port": 5050
11+
}
12+
]
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "Wait Remote Debugger Server",
6+
"type": "shell",
7+
"command": "while [[ -z $(docker ps | grep :5050) ]]; do sleep 1; done; sleep 1;"
8+
}
9+
]
10+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
export AWS_ACCESS_KEY_ID ?= test
2+
export AWS_SECRET_ACCESS_KEY ?= test
3+
export AWS_DEFAULT_REGION = us-east-1
4+
VENV_DIR ?= .venv
5+
6+
# https://hub.docker.com/_/gradle/tags
7+
IMAGE ?= gradle:8.4.0-jdk17
8+
9+
usage: ## Show this help
10+
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'
11+
12+
install: build-docker
13+
14+
build-docker: ## Build the Lambda function zip in Docker
15+
docker run --rm -v "$$(pwd)/java-function:/app" $(IMAGE) bash -c "cd /app && ./gradlew buildZip"
16+
17+
build-local: ## Build the Lambda function zip locally
18+
cd java-function && ./gradlew buildZip
19+
20+
clean:
21+
rm -rf java-function/build
22+
23+
run: ## Deploy and invoke the Lambda container locally
24+
echo "Deploying Lambda locally"; \
25+
./run.sh; \
26+
echo "Done - test successfully finished."
27+
28+
start:
29+
LOCALSTACK_LAMBDA_DEBUG_MODE=1 \
30+
LOCALSTACK_LAMBDA_DEBUG_MODE_CONFIG_PATH=/tmp/lambda_debug_mode_config.yaml \
31+
localstack start --volume ${PWD}/lambda_debug_mode_config.yaml:/tmp/lambda_debug_mode_config.yaml -d
32+
33+
stop:
34+
@echo
35+
localstack stop
36+
37+
ready:
38+
@echo Waiting on the LocalStack container...
39+
@localstack wait -t 30 && echo Localstack is ready to use! || (echo Gave up waiting on LocalStack, exiting. && exit 1)
40+
41+
logs:
42+
@localstack logs > logs.txt
43+
44+
test-ci:
45+
make install; return_code=`echo $$?`;\
46+
echo "Interactive debugging not tested in CI"; exit $$return_code;
47+
48+
.PHONY: usage install run start stop ready logs test-ci build-local build-docker clean
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# LocalStack Demo: Enable Lambda Debug Mode to Automatically Raise Execution Timeouts
2+
3+
A simple demo application showcasing how to debug Java Lambdas locally with Lambda Debug Mode.
4+
The demo deploys a Lambda function with a one-second timeout, which is automatically lifted when running LocalStack with Lambda Debug Mode enabled.
5+
6+
## Prerequisites
7+
8+
* LocalStack
9+
* Docker
10+
* `make`
11+
* [`awslocal`](https://github.com/localstack/awscli-local)
12+
* `java17` and `gradle` (optional for local build)
13+
14+
## Installing
15+
16+
To build the Lambda function archive:
17+
18+
```sh
19+
make install
20+
```
21+
22+
## Starting Up
23+
24+
Make sure that LocalStack is started with the following configuration:
25+
26+
```sh
27+
LOCALSTACK_LAMBDA_DEBUG_MODE=1 \
28+
LOCALSTACK_LAMBDA_DEBUG_MODE_CONFIG_PATH=/tmp/lambda_debug_mode_config.yaml \
29+
localstack start --volume $PWD/lambda_debug_mode_config.yaml:/tmp/lambda_debug_mode_config.yaml
30+
```
31+
32+
* `LOCALSTACK_LAMBDA_DEBUG_MODE=1` enables the Lambda debug mode
33+
* `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`.
34+
* `--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.
35+
36+
## Running the Sample
37+
38+
The project requires you to configure your IDE or editor of choice to debug remote Java Lambda functions on port 5050.
39+
[These documentations](https://docs.localstack.cloud/user-guide/lambda-tools/debugging/#debugging-jvm-lambdas) include a guide on how you can do so.
40+
41+
The following command used to deploy and invoke the Lambda locally:
42+
43+
```sh
44+
make run
45+
```
46+
47+
### Attaching the Remote Debugger
48+
49+
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.
50+
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.
51+
52+
## License
53+
54+
The code in this sample is available under the Apache 2.0 license.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
.gradle
2+
build/
3+
!gradle/wrapper/gradle-wrapper.jar
4+
!**/src/main/**/build/
5+
!**/src/test/**/build/
6+
7+
### IntelliJ IDEA ###
8+
.idea/modules.xml
9+
.idea/jarRepositories.xml
10+
.idea/compiler.xml
11+
.idea/libraries/
12+
*.iws
13+
*.iml
14+
*.ipr
15+
out/
16+
!**/src/main/**/out/
17+
!**/src/test/**/out/
18+
19+
### Eclipse ###
20+
.apt_generated
21+
.classpath
22+
.factorypath
23+
.project
24+
.settings
25+
.springBeans
26+
.sts4-cache
27+
bin/
28+
!**/src/main/**/bin/
29+
!**/src/test/**/bin/
30+
31+
### NetBeans ###
32+
/nbproject/private/
33+
/nbbuild/
34+
/dist/
35+
/nbdist/
36+
/.nb-gradle/
37+
38+
### VS Code ###
39+
.vscode/
40+
41+
### Mac OS ###
42+
.DS_Store
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
java {
6+
toolchain {
7+
languageVersion.set(JavaLanguageVersion.of(17))
8+
}
9+
}
10+
11+
12+
repositories {
13+
mavenCentral()
14+
}
15+
16+
dependencies {
17+
implementation 'com.amazonaws:aws-lambda-java-core:1.2.2'
18+
implementation 'com.amazonaws:aws-lambda-java-events:3.11.1'
19+
runtimeOnly 'com.amazonaws:aws-lambda-java-log4j2:1.5.1'
20+
}
21+
22+
tasks.register('buildZip', Zip) {
23+
into('lib') {
24+
from(jar)
25+
from(configurations.runtimeClasspath)
26+
}
27+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
6+
zipStoreBase=GRADLE_USER_HOME
7+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)