Skip to content

Add debugging setup & re-add release optimizations #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jobs:
go-version: '~1.18.2'

- name: Build
env:
RELEASE_BUILD_LINKER_FLAGS: "-s -w"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice to optimize the production version 👍

run: make compile-lambda-linux-all

- uses: actions/upload-artifact@v3
Expand Down
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ./...
Expand Down
1 change: 0 additions & 1 deletion custom-tests/.gitignore

This file was deleted.

16 changes: 0 additions & 16 deletions custom-tests/Makefile

This file was deleted.

8 changes: 0 additions & 8 deletions custom-tests/README.md

This file was deleted.

Binary file removed custom-tests/dlv
Binary file not shown.
File renamed without changes.
4 changes: 4 additions & 0 deletions debugging/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
aws-lambda-rie
build-delve/dlv
init/var/rapid/init
init/var/rapid/dlv
File renamed without changes.
45 changes: 45 additions & 0 deletions debugging/Makefile
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions debugging/README.md
Original file line number Diff line number Diff line change
@@ -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]`
File renamed without changes.
8 changes: 8 additions & 0 deletions debugging/build-delve/build.sh
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions debugging/init/var/rapid/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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