lambci / docker-lambda

Docker images and test runners that replicate the live AWS Lambda environment
MIT License
5.83k stars 431 forks source link

Help with Debugging Go Lambda #351

Open Balake opened 3 years ago

Balake commented 3 years ago

I've been having issues getting a vscode debugger to attach to the delve debugger on the lambci/lambda:go1.x container.

I haven't seen any documentation on how to do this. I've gone back through previous MR comments and have gleaned the following:

Here is the current command that I'm working with: docker run --rm -e DOCKER_LAMBDA_DEBUG='true' -e DOCKER_LAMBDA_STAY_OPEN=1 -p 9001:9001 -p 5985:5985 -v "$PWD":/var/task:ro,delegated -v "$DLV_PATH":/tmp/lambci_debug_files/dlv lambci/lambda:go1.x test -debug=true

Here is my vscode config:

{
  "name": "debug test",
  "type": "go",
  "mode": "remote",
  "port": 5985,
  "showLog": true,
  "request": "attach",
  "remotePath": "",
  "cwd": "{{path to test dir}}"
}

So far the debugger appears to attach (no errors), but I don't see any container logs indicating that dlv has even started (is there supposed to be?). If anyone has any insight as to what I'm doing wrong, it would be much appreciated. I'm also more than happy to contribute to some documentation in order to help others.

kynetiv commented 2 years ago

Hi @Balake, I was having this issue too and finally figured it out... and it turns out that the positional args in your docker run command (the arguments and flags after the image lambci/lambda:go1.x) needed to be reordered so that the aws-lambda-mock could parse the flags and the handler correctly. I think that the -debug=true flag was not being read because it wasn't first in the args list (your handler test came first).

Also, you'll know that dlv has started correctly when you see the API server listenaing at: [::]:5985 instead of port 9001.

Here's how I would reorder your command:

docker run --rm -e DOCKER_LAMBDA_STAY_OPEN=1 -p 9001:9001 -p 5985:5985 -v "$PWD":/var/task:ro,delegated -v "$DLV_PATH":/tmp/lambci_debug_files/dlv lambci/lambda:go1.x -debug=true test

I don't think DOCKER_LAMBDA_DEBUG='true' is needed.

Note: there is also a new flag for supporting the api version of delve, which you may want to add before your handler test arg: -delveAPI=2. The default is api-version=1.