Skip to content
This repository was archived by the owner on Jan 15, 2023. It is now read-only.

Delve Debugger in Golang Lambda Container - #97

Merged
mhart merged 1 commit into
lambci:masterfrom
austinlparker:go-debug-image
Jun 30, 2018
Merged

mhart merged 1 commit into
lambci:masterfrom
austinlparker:go-debug-image

Conversation

@austinlparker

Copy link
Copy Markdown
Contributor

This change does the following:

  • Adds dlv to the go lambda container image.
  • Adds a 'debug' lambda mock that invokes the function via delve in headless mode

This change is in support of aws/aws-sam-cli#281.

Please let me know if you'd prefer a different approach to this or have any other comments, thanks!

@mhart

mhart commented Jun 20, 2018

Copy link
Copy Markdown
Member

Hmmm, yeah the only thing I don't like is that there are extra binaries as /var/lang/bin/dlv and /var/runtime/aws-lambda-debug-go, which means this image deviates from production Lambda in terms of what's installed. (my main concern with that is ppl running code on docker-lambda that they think will run on a live Lambda)

That said, I'm not really sure what other options there are – when I was initially working on Go support, I asked @sanathkr, David Nasi and @bmoffatt about debugging approaches, including delve, but I don't think we got anywhere beyond "users will need to add it themselves".

Is there any way we can bundle delve together with the mock into a single /var/runtime/aws-lambda-go binary?

@austinlparker

Copy link
Copy Markdown
Contributor Author

Hm, not sure. We could probably bring it in as an import to aws-lambda-go and try to launch it via the API. I'll hack on it a bit more and see what I can come up with.

@austinlparker

Copy link
Copy Markdown
Contributor Author

It seems like it'd be pretty gnarly to bring in delve as a library and start the debugging service out of the aws-lambda-go binary. What I could do, though, is bring in just the dlv binary and make aws-lambda-go use a flag of some sort to debug the function that's passed in, rather than having a separate binary for it.

Another option would be to have a completely separate container tag - something like go1.x-dbg that included the additional binaries. That way there'd be a very clear line between the 'production' and 'non-production' version. Thoughts?

@austinlparker

Copy link
Copy Markdown
Contributor Author

Alright, I spent a little more time poking at this problem and here's the three paths forward -

  1. As-is; Have a single go1.x container image that includes delve and the debug wrapper.
  2. Separate 'debug' container. This would involve creating a new go1.xdebug container image that would have dlv and the debug handler from this PR as part of the container image.
  3. Mount delve into container from host. This is somewhat tricky because dlv needs to be recompiled for the container architecture but that's not the worst thing in the world. In this scenario, the dlv binary would be removed and the aws-lambda-go binary could be modified to support launching the handler via the debugger.

@mhart thoughts on which option is preferable? I think the third is probably the most elegant and preserves the 'production-like' nature of the container.

@austinlparker
austinlparker force-pushed the go-debug-image branch 3 times, most recently from 590a7dc to 595446f Compare June 21, 2018 20:19
@austinlparker

Copy link
Copy Markdown
Contributor Author

I got into it and decided that the third one was the best and refactored things a bit.

Good:

  • No new binaries added to the image.
  • Doesn't break existing consumers; The flags to enable delve are implemented via flags and have sane defaults, and they don't break parsing of positional arguments.

Less Good:

  • End-users will need to compile dlv themselves for GOARCH=amd64 GOOS=linux if they're running on MacOS or Windows.

I think the pros outweigh the cons.

@mhart

mhart commented Jun 21, 2018

Copy link
Copy Markdown
Member

Yeah, totally agree – that sounds like the best option. I'll look over it and see if any of the AWS ppl want to weigh in.

@sanathkr ?

@sanathkr sanathkr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This looks great!

Comment thread go1.x/run/aws-lambda-mock.go Outdated
rand.Seed(time.Now().UTC().UnixNano())

debugMode := flag.Bool("debug", false, "enables delve debugging")
delvePath := flag.String("delvePath", "/var/debug/dlv", "path to delve")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

/var/debug seems too generic. I don't want the container to break when official Lambda runtime suddenly makes use of this path in the future.

Instead, can you make it something explicit (ugly?) like /tmp/lambci_debug_files

@sanathkr sanathkr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for making the change! LGTM

Comment thread go1.x/run/Dockerfile
RUN dep ensure
RUN GOARCH=amd64 GOOS=linux go build aws-lambda-mock.go


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you revert this?

change default for local delve path to something more explicit

revert whitespace removal
@austinlparker

Copy link
Copy Markdown
Contributor Author

addressed all of the outstanding comments, squashed + pushed.

@mhart

mhart commented Jun 30, 2018

Copy link
Copy Markdown
Member

Awesome, merging :shipit:

@mhart
mhart merged commit 9181bc3 into lambci:master Jun 30, 2018
@austinlparker
austinlparker deleted the go-debug-image branch June 30, 2018 01:22
@mhart

mhart commented Jun 30, 2018

Copy link
Copy Markdown
Member

Hmmm, this seems to have broken some things.

docker run --rm -v "$PWD":/var/task lambci/lambda:go1.x handler '{"Records": []}'
START RequestId: 74efc08e-4288-126e-04a9-2d786ba70d5a Version: $LATEST
END RequestId: 74efc08e-4288-126e-04a9-2d786ba70d5a
REPORT RequestId: 74efc08e-4288-126e-04a9-2d786ba70d5a	Duration: 1.77 ms	Billed Duration: 100 ms	Memory Size: 1536 MB	Max Memory Used: 6 MB	
{
  "errorMessage": "fork/exec /var/task/{\"Records\": []}: no such file or directory",
  "errorType": "PathError"
}

I suspect it's the move from os.Args to flag.Args() – @austinlparker can you weigh in here?

@mhart

mhart commented Jun 30, 2018

Copy link
Copy Markdown
Member

Fixed in a4a89a5 (I think)

@czubocha

czubocha commented Nov 6, 2018 •

Copy link
Copy Markdown

@austinlparker Was Delve flag --api-version=1 added for some particular reason? I suppose at the time of creating this PR VS Code did not support Delve API in version 2. But now VS Code supports both and default is 2 so i think it will be better to switch. I came here because running Delve with --api-version=1 makes debugging Go Lambdas impossible at all in IntelliJ IDE (IDEA and Goland) as it uses version 2.

@austinlparker

Copy link
Copy Markdown
Contributor Author

At the time VSC didn't support Delve API v2, so I forced it to 1, that's the only reason IIRC. I'm sure they'd accept a PR for changing it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants