Skip to content

Commit

Permalink
add nodejs sample for lambda remote debugging (#205)
Browse files Browse the repository at this point in the history
* migrate the lambda remote debugging files to python directory

* add the nodejs sample
  • Loading branch information
HarshCasper authored Jan 18, 2023
1 parent af651db commit dd6939a
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 3 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ node_modules/
.classpath
.project
.settings/
.vscode/
target/

.idea/
Expand Down
15 changes: 15 additions & 0 deletions lambda-mounting-and-debugging/javascript/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "0.2.0",
"configurations": [
{
"address": "127.0.0.1",
"localRoot": "${workspaceFolder}",
"name": "Attach to Remote Node.js",
"port": 9229,
"remoteRoot": "/var/task/",
"request": "attach",
"type": "node",
"preLaunchTask": "Wait Remote Debugger Server"
},
]
}
10 changes: 10 additions & 0 deletions lambda-mounting-and-debugging/javascript/.vscode/tasks.json
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 :9229) ]]; do sleep 1; done; sleep 1;"
}
]
}
32 changes: 32 additions & 0 deletions lambda-mounting-and-debugging/javascript/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export AWS_ACCESS_KEY_ID ?= test
export AWS_SECRET_ACCESS_KEY ?= test
export AWS_DEFAULT_REGION = us-east-1
VENV_DIR ?= .venv

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

install: ## Install dependencies
@which awslocal || pip install awscli-local
test -e $(VENV_DIR) || virtualenv $(VENV_DIR)
. $(VENV_DIR)/bin/activate; pip install debugpy

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

start:
docker-compose up -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

.PHONY: usage install start run stop ready logs test-ci
48 changes: 48 additions & 0 deletions lambda-mounting-and-debugging/javascript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# LocalStack Demo: Lambda Code Mounting and Debugging

Simple demo application to illustrate debugging NodeJS Lambdas locally.

## Prerequisites

* LocalStack
* Docker
* `make`
* [`awslocal`](https://github.com/localstack/awscli-local)

## Installation

To install the dependencies:

```sh
make install
```

## Starting Up

You can start LocalStack with Docker Compose:

```sh
docker-compose up -d
```

Alternatively, you can use the following `localstack` CLI configuration:

```sh
LAMBDA_DOCKER_FLAGS='-e NODE_OPTIONS=--inspect-brk=0.0.0.0:9229 -p 9229:9229' \
LAMBDA_REMOTE_DOCKER=0 \
localstack start -d
```

## Running the sample

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.

The following command deploys the Lambda and finally invoke the Lambda locally:

```sh
make run
```

# License

The code in this sample is available under the Apache 2.0 license.
18 changes: 18 additions & 0 deletions lambda-mounting-and-debugging/javascript/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: "3.8"

services:
localstack:
container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
image: localstack/localstack:latest
ports:
- "127.0.0.1:4566:4566" # LocalStack Gateway
- "127.0.0.1:4510-4559:4510-4559" # external services port range
environment:
- DEBUG=1
- LAMBDA_REMOTE_DOCKER=0
- LAMBDA_DOCKER_FLAGS=-e NODE_OPTIONS=--inspect-brk=0.0.0.0:9229 -p 9229:9229
- LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR-}
- DOCKER_HOST=unix:///var/run/docker.sock
volumes:
- "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
8 changes: 8 additions & 0 deletions lambda-mounting-and-debugging/javascript/function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
exports.handler = async (event) => {
console.log(event);
const response = {
statusCode: 200,
body: "ok",
};
return response;
};
14 changes: 14 additions & 0 deletions lambda-mounting-and-debugging/javascript/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

awslocal lambda create-function \
--function-name localstack-nodejs-lambda-function \
--code S3Bucket="hot-reload",S3Key="$(pwd)/" \
--handler function.handler \
--runtime nodejs14.x \
--timeout 120 \
--role arn:aws:iam::000000000000:role/lambda-role

awslocal lambda invoke \
--function-name localstack-nodejs-lambda-function test.lambda.log \
--cli-binary-format raw-in-base64-out \
--payload '{"hello":"world"}'
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
]
}
]
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# LocalStack Demo: Lambda Code Mounting and Debugging

Simple demo application to illustrate debugging Lambdas locally.
Simple demo application to illustrate debugging Python Lambdas locally.

## Prerequisites

Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit dd6939a

Please sign in to comment.