Debug go lambda with Goland
by Afanasy Barbarov
Debugging Go AWS lambdas
And now we are back to the programming basics - debugging for go lambdas. Currently Goland IDE does not support debugging for go lambdas natively. Maybe it changes one day and this article become obsolete, who knows. Currently I managed to do a debug by running unit tests that run the handlers. One may say that this is not a true "go way", but I still remember the times, when there was no debugger for go at all, and debug was possible by adding some library to the project and inserting debug instructions to the code itself, which is weird in 2020.
There is an official guide is here: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-debugging-golang.html.
The guide suggests installing delve debugger, then run some commands. But, regarding the debugging in Goland, it means, that you already have everything installed.
Lets generate a new app by running sam init and selecting go1.x and Hello-World template. After you open the project in Goland you'll notice the green triangle next to func main(). Starting a debug session will give something like this:
/usr/local/Cellar/go/1.15.2/libexec/bin/go build
-o /private/var/folders/b7/dtfbb67x1sxgmcm490vtt8rm0000gn/T/
___go_build -gcflags all=-N -l hello-world #gosetup
"/Users/username/Library/Application
Support/JetBrains/Toolbox/apps/Goland/
ch-0/202.7319.61/GoLand.app/Contents/plugins/go/lib/dlv/mac/dlv" \
--listen=0.0.0.0:50909 \
--headless=true \
--api-version=2 \
--check-go-version=false \
--only-same-user=false \
exec /private/var/folders/b7/dtfbb67x1sxgmcm490vtt8rm0000gn/T/
___go_build --
API server listening at: [::]:50909So the Goland itself uses the remote debugging described in AWS guide. Trying to run the SAM locally and attaching to the debugger works, but doesn't hit the breakpont. I spent some time playing around and running all that magic, and seems that it is for true geeks only. For me easier is to run a unit test and invoke the desired handler directly. And if I need a real lambda running, I just run the lambda locally:)
So I'll wait when Goland natively supports go1.x with its AWS Toolkit plugin. And before that - unit testing is my best friend.
No magic and workarounds this time.
That's all, folks!