Running and debugging Go Lambda functions locally
--
A solution without CLIs, Docker or big frameworks; just a binary, a debugger and simple configs. This article is also available on my blog.
Debugging locally a lambda function written in Go is not a trivial task, this article is my trial at this subject, it represents the result of a week of intense research and frustration pursuing a thing that should be trivial for any developer: running your code on your machine and attaching a debugger to it. This setup it great for development and essential for building high quality software, but even after days of effort I wasn’t able to properly step-through my code, so I gave up and decided to do thing the old way.
After trying various approaches with recommended tools like aws-sam-cli and the serverless-framework with no success, I ended up with a very simple setup that let me step-through my code, check values and dig into the function execution, with the real debugging, and it was adopted in the back-end team at my company. So here it goes.
The setup is basically the following:
- Build your lambda function with debugging symbols
- Run it and attach the debugger
- Make a RPC call to it using a client (included here)
- Follow the code execution on visual studio
Needed software
- go package delve, install it running:
go get github.com/go-delve/delve/cmd/dlv
- go package awslambdarpc, install running
go get github.com/blmayer/awslambdarpc
- I used visual studio code but it should work on other IDEs
- And for convenience and speed, the file-picker VSCode extension
Make sure your $GOPATH/bin folder is in your PATH so that VSCode can find them.
Before we start
Just a little brief, a lambda function is basically a RPC (remote procedure call) server, and RPC servers work in a different way: they advertise methods that they have available, and clients call these methods passing its name and the arguments needed, if any. In a lambda function the function exposed is called Invoke()
and it’s defined on the Function
type, so the method called is…