Skip to content

Commit bb7bdaa

Browse files
committed
Add volume mounts and improve sample scripts
1 parent eda0d28 commit bb7bdaa

File tree

10 files changed

+116
-122
lines changed

10 files changed

+116
-122
lines changed

lambda-debug-mode/python/base-concurrent-lambda-debug-mode/Makefile

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,24 @@ run: ## Deploy and invoke the Lambda container locally
1616
./run.sh; \
1717
echo "Done - test successfully finished."
1818

19-
.PHONY: usage install run
19+
start:
20+
LOCALSTACK_LAMBDA_DEBUG_MODE=1 \
21+
LOCALSTACK_LAMBDA_DEBUG_MODE_CONFIG_PATH=/tmp/lambda_debug_mode_config.yaml \
22+
localstack start --volume ${PWD}/lambda_debug_mode_config.yaml:/tmp/lambda_debug_mode_config.yaml -d
2023

24+
stop:
25+
@echo
26+
localstack stop
27+
28+
ready:
29+
@echo Waiting on the LocalStack container...
30+
@localstack wait -t 30 && echo Localstack is ready to use! || (echo Gave up waiting on LocalStack, exiting. && exit 1)
31+
32+
logs:
33+
@localstack logs > logs.txt
34+
35+
test-ci:
36+
make install; return_code=`echo $$?`;\
37+
echo "Interactive debugging not tested in CI"; exit $$return_code;
38+
39+
.PHONY: usage install run start stop ready logs test-ci
Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# LocalStack Demo: Lambda Debug Mode Automatically Handle Concurrent Function Invocations
22

3-
A simple demo application showcasing how to Lambda Debug Mode automatically controls concurrent lambda call invocations.
3+
A simple demo application showcasing how Lambda Debug Mode automatically controls concurrent lambda call invocations.
44
The demo deploys a Lambda function with a one-second timeout, and continues to invoke this function
55
three times. Upon connecting the remote debugger, the user should see how only the first call is received and can be
66
debugged, with concurrency error messages being logged about the following two invocations.
@@ -15,43 +15,40 @@ debugged, with concurrency error messages being logged about the following two i
1515
## Installing
1616

1717
To install the dependencies:
18-
```
18+
19+
```sh
1920
make install
2021
```
2122

2223
## Starting Up
2324

24-
Make sure that LocalStack is started with the following configuration:
25+
```sh
26+
LOCALSTACK_LAMBDA_DEBUG_MODE=1 \
27+
LOCALSTACK_LAMBDA_DEBUG_MODE_CONFIG_PATH=/tmp/lambda_debug_mode_config.yaml \
28+
localstack start --volume $PWD/lambda_debug_mode_config.yaml:/tmp/lambda_debug_mode_config.yaml
2529
```
26-
LAMBDA_DEBUG_MODE=1 \
27-
LAMBDA_DEBUG_MODE_CONFIG_PATH=path/to/lambda_debug_mode_config.yaml \
28-
localstack start
29-
```
30-
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 `19891`.
3530

31+
* `LOCALSTACK_LAMBDA_DEBUG_MODE=1` enables the Lambda debug mode
32+
* `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 `19891`.
33+
* `--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.
3634

3735
## Running the Sample
3836

3937
The project ships with a Visual Studio Code debug launch config (see `.vscode/launch.json`). This configuration can be used to attach to the code in the Lambda function while it is executing.
4038

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

43-
```
41+
```sh
4442
make run
4543
```
4644

4745
### Attaching the VSCode Debugger
4846

49-
After the Lambda function is invoked you can switch to Visual Studio Code, set a breakpoint in the Lambda handler, and run the preconfigured remote debugger.
47+
After the Lambda function is invoked you can switch to Visual Studio Code, set a breakpoint in the Lambda handler, and run the pre-configured remote debugger.
5048
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.
5149
You should also notice how the debugger connects to the first invocation (the message object would end with `Attempt 1`), whilst the following invocations
5250
are refused automatically by Lambda Debug Mode as explained in the corresponding log messages.
5351

5452
## License
5553

5654
The code in this sample is available under the Apache 2.0 license.
57-

lambda-debug-mode/python/base-concurrent-lambda-debug-mode/run.sh

100644100755
Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,9 @@ awslocal lambda create-function \
1111
--code "S3Bucket=hot-reload,S3Key=$(pwd)/" \
1212
--handler handler.handler \
1313
--role arn:aws:iam::000000000000:role/test-role \
14-
--runtime python3.9
14+
--runtime python3.12
1515

16-
# Function to check the status of the Lambda function.
17-
check_lambda_status() {
18-
status=$(awslocal lambda get-function --function-name "$FUNCTION_NAME" 2>&1)
19-
# Check if "Active" is in the response
20-
if echo "$status" | grep -q "Active"; then
21-
return 0
22-
fi
23-
return 1
24-
}
25-
26-
# Wait until the Lambda function is active
27-
echo "Waiting for Lambda function to become active..."
28-
while true; do
29-
if check_lambda_status; then
30-
echo "Lambda function is active."
31-
break
32-
else
33-
echo "Lambda function is still pending. Waiting..."
34-
sleep 1
35-
fi
36-
done
16+
awslocal lambda wait function-active-v2 --function-name "$FUNCTION_NAME"
3717

3818
# Invoke the Lambda function 3 times every 5 seconds.
3919
for i in {1..3}; do
@@ -42,9 +22,10 @@ for i in {1..3}; do
4222
awslocal lambda invoke \
4323
--cli-connect-timeout 3600 \
4424
--cli-read-timeout 3600 \
45-
--function-name "function_one" \
25+
--function-name "$FUNCTION_NAME" \
4626
--payload "{\"message\": \"Testing Lambda Debug Mode lifting the 1-second timeout for function-one. Attempt $i.\"}" \
4727
/dev/stdout 2>/dev/stderr &
4828
sleep 5
4929
done
5030

31+
echo "Set a breakpoint and attach the Python remote debugger from your IDE"

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,24 @@ run: ## Deploy and invoke the Lambda container locally
1616
./run.sh; \
1717
echo "Done - test successfully finished."
1818

19-
.PHONY: usage install run
19+
start:
20+
LOCALSTACK_LAMBDA_DEBUG_MODE=1 \
21+
LOCALSTACK_LAMBDA_DEBUG_MODE_CONFIG_PATH=/tmp/lambda_debug_mode_config.yaml \
22+
localstack start --volume ${PWD}/lambda_debug_mode_config.yaml:/tmp/lambda_debug_mode_config.yaml -d
2023

24+
stop:
25+
@echo
26+
localstack stop
27+
28+
ready:
29+
@echo Waiting on the LocalStack container...
30+
@localstack wait -t 30 && echo Localstack is ready to use! || (echo Gave up waiting on LocalStack, exiting. && exit 1)
31+
32+
logs:
33+
@localstack logs > logs.txt
34+
35+
test-ci:
36+
make install; return_code=`echo $$?`;\
37+
echo "Interactive debugging not tested in CI"; exit $$return_code;
38+
39+
.PHONY: usage install run start stop ready logs test-ci

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,46 @@ The demo deploys a Lambda function with a one-second timeout, which is automatic
1313
## Installing
1414

1515
To install the dependencies:
16-
```
16+
17+
```sh
1718
make install
1819
```
1920

2021
## Starting Up
2122

2223
Make sure that LocalStack is started with the following configuration:
23-
```
24-
LAMBDA_DEBUG_MODE=1 \
25-
LAMBDA_DEBUG_MODE_CONFIG_PATH=path/to/lambda_debug_mode_config.yaml \
26-
localstack start
27-
```
28-
29-
Lambda Debug Mode is enabled through the config option `LAMBDA_DEBUG_MODE=1`.
3024

31-
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`.
32-
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 `19891`.
25+
```sh
26+
LOCALSTACK_LAMBDA_DEBUG_MODE=1 \
27+
LOCALSTACK_LAMBDA_DEBUG_MODE_CONFIG_PATH=/tmp/lambda_debug_mode_config.yaml \
28+
localstack start --volume $PWD/lambda_debug_mode_config.yaml:/tmp/lambda_debug_mode_config.yaml
29+
```
3330

31+
* `LOCALSTACK_LAMBDA_DEBUG_MODE=1` enables the Lambda debug mode
32+
* `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 `19891`.
33+
* `--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.
3434

3535
## Running the Sample
3636

3737
The project ships with a Visual Studio Code debug launch config (see `.vscode/launch.json`). This configuration can be used to attach to the code in the Lambda function while it is executing.
3838

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

41-
```
41+
```sh
4242
make run
4343
```
4444

4545
### Attaching the VSCode Debugger
4646

47-
After the Lambda function is invoked you can switch to Visual Studio Code, set a breakpoint in the Lambda handler, and run the preconfigured remote debugger.
47+
After the Lambda function is invoked you can switch to Visual Studio Code, set a breakpoint in the Lambda handler, and run the pre-configured remote debugger.
4848
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.
4949

5050
### Quick Dev Loop with Lambda Code Mounting
5151

5252
Note that, since the Lambda code is mounted from your local filesystem into the Lambda container (by means of `hot-reload` as special bucket name in `run.sh`),
5353
all changes are immediately reflected. For example, you could change the implementation of the handler as follows:
54-
```
54+
55+
```python
5556
def handler(event, context):
5657
"""Lambda handler that will get invoked by the LocalStack runtime"""
5758

@@ -64,13 +65,13 @@ def handler(event, context):
6465
# Additional line added below:
6566
print("!! Additional log output !!")
6667

67-
# Return the incomeing invocation evant.
68+
# Return the incoming invocation event.
6869
return event
6970
```
71+
7072
and then upon next invocation of the Lambda, the additional print output will immediately appear in the Lambda logs.
7173
This allows for a quick dev/debug loop, without the need to redeploy the Lambda after the handler is changed!
7274

7375
## License
7476

7577
The code in this sample is available under the Apache 2.0 license.
76-

lambda-debug-mode/python/base-enable-lambda-debug-mode/run.sh

100644100755
Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,18 @@ awslocal lambda create-function \
1111
--code "S3Bucket=hot-reload,S3Key=$(pwd)/" \
1212
--handler handler.handler \
1313
--role arn:aws:iam::000000000000:role/test-role \
14-
--runtime python3.9
14+
--runtime python3.12
1515

16-
# Function to check the status of the Lambda function.
17-
check_lambda_status() {
18-
status=$(awslocal lambda get-function --function-name "$FUNCTION_NAME" 2>&1)
19-
# Check if "Active" is in the response
20-
if echo "$status" | grep -q "Active"; then
21-
return 0
22-
fi
23-
return 1
24-
}
25-
26-
# Wait until the Lambda function is active
27-
echo "Waiting for Lambda function to become active..."
28-
while true; do
29-
if check_lambda_status; then
30-
echo "Lambda function is active."
31-
break
32-
else
33-
echo "Lambda function is still pending. Waiting..."
34-
sleep 1
35-
fi
36-
done
16+
awslocal lambda wait function-active-v2 --function-name "$FUNCTION_NAME"
3717

3818
# Invoke the Lambda function.
3919
echo "Invoking the Lambda function."
4020
AWS_MAX_ATTEMPTS=1 \
4121
awslocal lambda invoke \
4222
--cli-connect-timeout 3600 \
4323
--cli-read-timeout 3600 \
44-
--function-name "function_one" \
24+
--function-name "$FUNCTION_NAME" \
4525
--payload '{"message": "Testing Lambda Debug Mode lifting the 1-second timeout for function-one."}' \
4626
/dev/stdout 2>/dev/stderr
4727

28+
echo "Set a breakpoint and attach the Python remote debugger from your IDE"

lambda-debug-mode/python/base-multiple-lambda-debug-mode/Makefile

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,24 @@ run: ## Deploy and invoke the Lambda container locally
1616
./run.sh; \
1717
echo "Done - test successfully finished."
1818

19-
.PHONY: usage install run
19+
start:
20+
LOCALSTACK_LAMBDA_DEBUG_MODE=1 \
21+
LOCALSTACK_LAMBDA_DEBUG_MODE_CONFIG_PATH=/tmp/lambda_debug_mode_config.yaml \
22+
localstack start --volume ${PWD}/lambda_debug_mode_config.yaml:/tmp/lambda_debug_mode_config.yaml -d
2023

24+
stop:
25+
@echo
26+
localstack stop
27+
28+
ready:
29+
@echo Waiting on the LocalStack container...
30+
@localstack wait -t 30 && echo Localstack is ready to use! || (echo Gave up waiting on LocalStack, exiting. && exit 1)
31+
32+
logs:
33+
@localstack logs > logs.txt
34+
35+
test-ci:
36+
make install; return_code=`echo $$?`;\
37+
echo "Interactive debugging not tested in CI"; exit $$return_code;
38+
39+
.PHONY: usage install run start stop ready logs test-ci

lambda-debug-mode/python/base-multiple-lambda-debug-mode/README.md

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,48 @@ and configure the Docker container to open specific debug ports for the two lamb
1414
## Installing
1515

1616
To install the dependencies:
17-
```
17+
18+
```sh
1819
make install
1920
```
2021

2122
## Starting Up
2223

2324
Make sure that LocalStack is started with the following configuration:
24-
```
25-
LAMBDA_DEBUG_MODE=1 \
26-
LAMBDA_DEBUG_MODE_CONFIG_PATH=path/to/lambda_debug_mode_config.yaml \
27-
localstack start
28-
```
29-
30-
Lambda Debug Mode is enabled through the config option `LAMBDA_DEBUG_MODE=1`.
3125

32-
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`.
33-
The config file contains instructions for Lambda Debug Mode to debug
34-
Lambda function `arn:aws:lambda:us-east-1:000000000000:function:function-one` on port `19891` and
35-
Lambda function `arn:aws:lambda:us-east-1:000000000000:function:function-two` on port `19892`.
36-
The two lambda functions configure `debugpy` to listen for debug connections on the corresponding ports.
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+
```
3731

32+
* `LOCALSTACK_LAMBDA_DEBUG_MODE=1` enables the Lambda debug mode
33+
* `LOCALSTACK_LAMBDA_DEBUG_MODE_CONFIG_PATH` 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 `19891` and
34+
Lambda function `arn:aws:lambda:us-east-1:000000000000:function:function-two` on port `19892`. The two lambda functions configure `debugpy` to listen for debug connections on the corresponding ports.
35+
* `--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.
3836

3937
## Running the Sample
4038

4139
The project ships with a Visual Studio Code debug launch config (see `.vscode/launch.json`). This configuration can be used to attach to the code in the Lambda function while it is executing.
4240

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

45-
```
43+
```sh
4644
make run
4745
```
4846

4947
### Attaching the VSCode Debugger
5048

51-
After the Lambda function is invoked you can switch to Visual Studio Code, set a breakpoint in the Lambda handler, and run the preconfigured remote debuggers.
52-
You will find that you can run the two preconfigured remote debuggers at the same time and simultaneously debug both Lambda functions.
49+
After the Lambda function is invoked you can switch to Visual Studio Code, set a breakpoint in the Lambda handler, and run the pre-configured remote debuggers.
50+
You will find that you can run the two pre-configured remote debuggers at the same time and simultaneously debug both Lambda functions.
5351
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.
5452

5553
### Quick Dev Loop with Lambda Code Mounting
5654

5755
Note that, since the Lambda code is mounted from your local filesystem into the Lambda container (by means of `hot-reload` as special bucket name in `run.sh`),
5856
all changes are immediately reflected. For example, you could change the implementation of the handler for `function_one` as follows:
59-
```
57+
58+
```python
6059
def handler(event, context):
6160
"""Lambda handler that will get invoked by the LocalStack runtime"""
6261

@@ -72,7 +71,7 @@ def handler(event, context):
7271
# Additional line added below:
7372
print("!! Additional log output !!")
7473

75-
# Return the incomeing invocation evant.
74+
# Return the incoming invocation event.
7675
return event
7776
```
7877

@@ -82,4 +81,3 @@ This allows for a quick dev/debug loop, without the need to redeploy the Lambda
8281
## License
8382

8483
The code in this sample is available under the Apache 2.0 license.
85-

lambda-debug-mode/python/base-multiple-lambda-debug-mode/handler_function_two.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,3 @@ def run(self):
3535

3636
if __name__ == "__main__":
3737
handler({}, {})
38-

0 commit comments

Comments
 (0)