diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 40a2769..518aa03 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -20,6 +20,8 @@ jobs: go-version: '~1.18.2' - name: Build + env: + RELEASE_BUILD_LINKER_FLAGS: "-s -w" run: make compile-lambda-linux-all - uses: actions/upload-artifact@v3 diff --git a/Makefile b/Makefile index 22e7cdb..d1b4113 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,6 @@ # RELEASE_BUILD_LINKER_FLAGS disables DWARF and symbol table generation to reduce binary size #RELEASE_BUILD_LINKER_FLAGS=-s -w - BINARY_NAME=aws-lambda-rie ARCH=x86_64 GO_ARCH_x86_64 := amd64 @@ -25,7 +24,7 @@ compile-with-docker: docker run --rm --env GOPROXY=direct -v $(shell pwd):/LambdaRuntimeLocal -w /LambdaRuntimeLocal golang:1.18 make ARCH=${ARCH} compile-lambda-linux compile-lambda-linux: - CGO_ENABLED=0 GOOS=linux GOARCH=${GO_ARCH_${ARCH}} go build -ldflags "${RELEASE_BUILD_LINKER_FLAGS}" -gcflags="all=-N -l" -o ${DESTINATION_${ARCH}} ./cmd/localstack + CGO_ENABLED=0 GOOS=linux GOARCH=${GO_ARCH_${ARCH}} go build -ldflags "${RELEASE_BUILD_LINKER_FLAGS}" -gcflags="${GC_FLAGS}" -o ${DESTINATION_${ARCH}} ./cmd/localstack tests: go test ./... diff --git a/custom-tests/.gitignore b/custom-tests/.gitignore deleted file mode 100644 index 779f096..0000000 --- a/custom-tests/.gitignore +++ /dev/null @@ -1 +0,0 @@ -aws-lambda-rie \ No newline at end of file diff --git a/custom-tests/Makefile b/custom-tests/Makefile deleted file mode 100644 index 4e2d325..0000000 --- a/custom-tests/Makefile +++ /dev/null @@ -1,16 +0,0 @@ -build: - docker build -t rapid-debug . - -run: - make build - docker run -d -p 9000:8080 --cap-add=SYS_PTRACE rapid-debug:latest - -clean: - rm -f aws-lambda-rie - docker rm -f rapid-debug - -debug-run: clean build - cd .. && make compile-lambda-linux - mv ../bin/aws-lambda-rie-x86_64 ./aws-lambda-rie - docker run --cap-add=SYS_PTRACE -d -v $(shell pwd)/dlv:/dlv -v $(shell pwd)/lambda-entrypoint.sh:/lambda-entrypoint.sh -v $(shell pwd)/aws-lambda-rie:/usr/local/bin/aws-lambda-rie -p 9000:8080 -p 9001:8081 -p 40000:40000 -p 9563:9563 --env LOCALSTACK_RIE_DEBUG_PORT=40000 --name rapid-debug rapid-debug:latest - diff --git a/custom-tests/README.md b/custom-tests/README.md deleted file mode 100644 index 9f1ec28..0000000 --- a/custom-tests/README.md +++ /dev/null @@ -1,8 +0,0 @@ - -## Debugging - -Debugging with delve can be enabled by setting the entrypoint to the delve binary instead: - -```bash -/dlv --listen=:"${LOCALSTACK_RIE_DEBUG_PORT}" --headless=true --api-version=2 --accept-multiclient exec /usr/local/bin/aws-lambda-rie "${RUNTIME_ENTRYPOINT}" -``` \ No newline at end of file diff --git a/custom-tests/dlv b/custom-tests/dlv deleted file mode 100755 index 272b33b..0000000 Binary files a/custom-tests/dlv and /dev/null differ diff --git a/custom-tests/.dockerignore b/debugging/.dockerignore similarity index 100% rename from custom-tests/.dockerignore rename to debugging/.dockerignore diff --git a/debugging/.gitignore b/debugging/.gitignore new file mode 100644 index 0000000..1553eae --- /dev/null +++ b/debugging/.gitignore @@ -0,0 +1,4 @@ +aws-lambda-rie +build-delve/dlv +init/var/rapid/init +init/var/rapid/dlv \ No newline at end of file diff --git a/custom-tests/Dockerfile b/debugging/Dockerfile similarity index 100% rename from custom-tests/Dockerfile rename to debugging/Dockerfile diff --git a/debugging/Makefile b/debugging/Makefile new file mode 100644 index 0000000..03ea056 --- /dev/null +++ b/debugging/Makefile @@ -0,0 +1,45 @@ +# Golang EOL overview: https://endoflife.date/go +DOCKER_GOLANG_IMAGE ?= golang:1.18.2 + +# On ARM hosts, use: make ARCH=arm64 build-init +# Check host architecture: uname -m +# x86_64 or arm64 +ARCH ?= x86_64 + +ifeq ($(ARCH), arm64) + GOARCH=arm64 +else + GOARCH=amd64 +endif + +# Limitation: Debugging x86_64 lambdas does not work on ARM machines due to missing ptrace implementation in qemu used by Docker +# The function aborts with "could not launch process: fork/exec /var/rapid/init: function not implemented" +# * Discussion: https://github.com/aws/aws-sam-cli/discussions/4706 +# * Docker for Mac: https://github.com/docker/for-mac/issues/5191#issuecomment-834154431 + + +all: build + +# build & "package" necessary files for debugging +build build-init: build-rapid build-delve/dlv + cp ../bin/aws-lambda-rie-$(ARCH) ./init/var/rapid/init + cp ./build-delve/dlv ./init/var/rapid/dlv + +build-rapid: + cd .. && GC_FLAGS="all=-N -l" make ARCH=$(ARCH) compile-lambda-linux + +build-delve/dlv: + docker run --rm -v $$(pwd)/build-delve/:/app/ $(DOCKER_GOLANG_IMAGE) \ + bash -c "cd /app && export GOARCH=$(GOARCH) && ./build.sh" + +clean clean-init: clean-rapid clean-delve + +clean-rapid: + rm -rf ../bin/aws-lambda-rie-* + rm -rf ./init/var/rapid/init + +clean-delve: + rm -rf ./build-delve/dlv + rm -rf ./init/var/rapid/dlv + +.PHONY: build build-rapid clean clean-rapid clean-delve diff --git a/debugging/README.md b/debugging/README.md new file mode 100644 index 0000000..ac62d31 --- /dev/null +++ b/debugging/README.md @@ -0,0 +1,36 @@ +# Testing lambda-runtime-init + +## Testing in isolation +Useful if you want more control over the API between the init and LocalStack (e.g. for error responses) + + +## Debugging with LocalStack + +1. Build init via `make build` + * On ARM hosts, use `make ARCH=arm64 build` + +2. Start LocalStack with the following flags: + + ``` + LAMBDA_INIT_BIN_PATH=/Users/joe/Projects/LocalStack/lambda-runtime-init/custom-tests/init/var/rapid/init + LAMBDA_INIT_BOOTSTRAP_PATH=/Users/joe/Projects/LocalStack/lambda-runtime-init/custom-tests/init/var/rapid/entrypoint.sh + LAMBDA_INIT_DEBUG=1 + LAMBDA_INIT_DELVE_PATH=/Users/joe/Projects/LocalStack/lambda-runtime-init/custom-tests/init/var/rapid/dlv + LAMBDA_INIT_DELVE_PORT=40000 + LAMBDA_RUNTIME_ENVIRONMENT_TIMEOUT=3600 + TEST_DISABLE_RETRIES_AND_TIMEOUTS=1 + ``` + + * `LAMBDA_INIT_DEBUG=1|0` enables or disables RIE copying and debugging. + * `LAMBDA_REMOVE_CONTAINERS=0` keeps exited containers + * Adjust the path to `lambda-runtime-init` accordingly + +3. Start Delve debugger and connect to `localhost:40000` + + +### Advice for function configuration + +Within `create_lambda_function`: + +* Increase the `timeout=3600` +* On ARM hosts, use `Architectures=[Architecture.arm64]` diff --git a/custom-tests/app.py b/debugging/app.py similarity index 100% rename from custom-tests/app.py rename to debugging/app.py diff --git a/debugging/build-delve/build.sh b/debugging/build-delve/build.sh new file mode 100755 index 0000000..69f6a2b --- /dev/null +++ b/debugging/build-delve/build.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +mkdir -p /tmp/build-delve +cd /tmp/build-delve +git clone https://github.com/go-delve/delve.git +cd delve +make build +mv dlv /app/dlv \ No newline at end of file diff --git a/debugging/init/var/rapid/entrypoint.sh b/debugging/init/var/rapid/entrypoint.sh new file mode 100755 index 0000000..c0c01cd --- /dev/null +++ b/debugging/init/var/rapid/entrypoint.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +LAMBDA_INIT_DELVE_PORT="${LAMBDA_INIT_DELVE_PORT:-40000}" + +# Run init without delve debugger +#exec /var/rapid/init + +exec /var/rapid/dlv --listen=:${LAMBDA_INIT_DELVE_PORT} --headless=true --api-version=2 --accept-multiclient exec /var/rapid/init