Skip to content

Commit 5ed3799

Browse files
committed
Add Docker build for Java sample
1 parent 1bd640d commit 5ed3799

File tree

18 files changed

+208
-153
lines changed

18 files changed

+208
-153
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+
}

lambda-debug-mode/java/base-enable-lambda-debug-mode/Makefile

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,46 @@ export AWS_SECRET_ACCESS_KEY ?= test
33
export AWS_DEFAULT_REGION = us-east-1
44
VENV_DIR ?= .venv
55

6+
# https://hub.docker.com/_/gradle/tags
7+
IMAGE ?= gradle:8.4.0-jdk17
8+
69
usage: ## Show this help
710
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'
811

9-
build-zip: ## Build the Lambda funzion zip.
10-
cd java-lambda-function/base-enable-lambda-debug-mode/ && ./gradlew buildZip
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
1122

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

17-
.PHONY: usage install run
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;
1847

48+
.PHONY: usage install run start stop ready logs test-ci build-local build-docker clean

lambda-debug-mode/java/base-enable-lambda-debug-mode/README.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,30 @@ The demo deploys a Lambda function with a one-second timeout, which is automatic
88
* LocalStack
99
* Docker
1010
* `make`
11-
* `gradle`
12-
* `java17`
1311
* [`awslocal`](https://github.com/localstack/awscli-local)
12+
* `java17` and `gradle` (optional for local build)
1413

1514
## Installing
1615

17-
To build the Lambda funcion archive:
18-
```
19-
make build-zip
16+
To build the Lambda function archive:
17+
18+
```sh
19+
make install
2020
```
2121

2222
## Starting Up
2323

2424
Make sure that LocalStack is started with the following configuration:
25-
```
26-
LAMBDA_DEBUG_MODE=1 \
27-
LAMBDA_DEBUG_MODE_CONFIG_PATH=path/to/lambda_debug_mode_config.yaml \
28-
localstack start
29-
```
3025

31-
Lambda Debug Mode is enabled through the config option `LAMBDA_DEBUG_MODE=1`.
32-
33-
The config option `LAMBDA_DEBUG_MODE_CONFIG_PATH` should point to the provided `yaml` config file for Lambda Debug Mode `lambda_debug_mode_config.yaml`.
34-
The config file contains instructions for Lambda Debug Mode to debug the Lambda function `arn:aws:lambda:us-east-1:000000000000:function:function-one` on port `5050`.
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+
```
3531

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.
3635

3736
## Running the Sample
3837

@@ -41,7 +40,7 @@ The project requires you to configure your IDE or editor of choice to debug remo
4140

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

44-
```
43+
```sh
4544
make run
4645
```
4746

@@ -53,4 +52,3 @@ LocalStack will automatically waive the set one second timeout for the Lambda fu
5352
## License
5453

5554
The code in this sample is available under the Apache 2.0 license.
56-

lambda-debug-mode/java/base-enable-lambda-debug-mode/java-lambda-function/base-enable-lambda-debug-mode/.gitignore renamed to lambda-debug-mode/java/base-enable-lambda-debug-mode/java-function/.gitignore

File renamed without changes.

lambda-debug-mode/java/base-enable-lambda-debug-mode/java-lambda-function/base-enable-lambda-debug-mode/build.gradle renamed to lambda-debug-mode/java/base-enable-lambda-debug-mode/java-function/build.gradle

File renamed without changes.

lambda-debug-mode/java/base-enable-lambda-debug-mode/java-lambda-function/base-enable-lambda-debug-mode/gradle/wrapper/gradle-wrapper.properties renamed to lambda-debug-mode/java/base-enable-lambda-debug-mode/java-function/gradle/wrapper/gradle-wrapper.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#Wed Sep 04 15:04:29 CEST 2024
21
distributionBase=GRADLE_USER_HOME
32
distributionPath=wrapper/dists
4-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
56
zipStoreBase=GRADLE_USER_HOME
67
zipStorePath=wrapper/dists

lambda-debug-mode/java/base-enable-lambda-debug-mode/java-lambda-function/base-enable-lambda-debug-mode/gradlew renamed to lambda-debug-mode/java/base-enable-lambda-debug-mode/java-function/gradlew

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
1717
#
18+
# SPDX-License-Identifier: Apache-2.0
19+
#
1820

1921
##############################################################################
2022
#
@@ -55,7 +57,7 @@
5557
# Darwin, MinGW, and NonStop.
5658
#
5759
# (3) This script is generated from the Groovy template
58-
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
60+
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
5961
# within the Gradle project.
6062
#
6163
# You can find Gradle at https://github.com/gradle/gradle/.
@@ -80,13 +82,12 @@ do
8082
esac
8183
done
8284

83-
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
84-
85-
APP_NAME="Gradle"
85+
# This is normally unused
86+
# shellcheck disable=SC2034
8687
APP_BASE_NAME=${0##*/}
87-
88-
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
89-
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
88+
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
89+
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
90+
' "$PWD" ) || exit
9091

9192
# Use the maximum available, or set MAX_FD != -1 to use that value.
9293
MAX_FD=maximum
@@ -133,22 +134,29 @@ location of your Java installation."
133134
fi
134135
else
135136
JAVACMD=java
136-
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
137+
if ! command -v java >/dev/null 2>&1
138+
then
139+
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
137140
138141
Please set the JAVA_HOME variable in your environment to match the
139142
location of your Java installation."
143+
fi
140144
fi
141145

142146
# Increase the maximum file descriptors if we can.
143147
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
144148
case $MAX_FD in #(
145149
max*)
150+
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
151+
# shellcheck disable=SC2039,SC3045
146152
MAX_FD=$( ulimit -H -n ) ||
147153
warn "Could not query maximum file descriptor limit"
148154
esac
149155
case $MAX_FD in #(
150156
'' | soft) :;; #(
151157
*)
158+
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
159+
# shellcheck disable=SC2039,SC3045
152160
ulimit -n "$MAX_FD" ||
153161
warn "Could not set maximum file descriptor limit to $MAX_FD"
154162
esac
@@ -193,18 +201,28 @@ if "$cygwin" || "$msys" ; then
193201
done
194202
fi
195203

196-
# Collect all arguments for the java command;
197-
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
198-
# shell script including quotes and variable substitutions, so put them in
199-
# double quotes to make sure that they get re-expanded; and
200-
# * put everything else in single quotes, so that it's not re-expanded.
204+
205+
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
206+
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
207+
208+
# Collect all arguments for the java command:
209+
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
210+
# and any embedded shellness will be escaped.
211+
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
212+
# treated as '${Hostname}' itself on the command line.
201213

202214
set -- \
203215
"-Dorg.gradle.appname=$APP_BASE_NAME" \
204216
-classpath "$CLASSPATH" \
205217
org.gradle.wrapper.GradleWrapperMain \
206218
"$@"
207219

220+
# Stop when "xargs" is not available.
221+
if ! command -v xargs >/dev/null 2>&1
222+
then
223+
die "xargs is not available"
224+
fi
225+
208226
# Use "xargs" to parse quoted args.
209227
#
210228
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.

0 commit comments

Comments
 (0)