Debugging AWS Lambda Functions in Go with VSCode

From time to time developers would need to understand how their code behaves across different environments for different reasons. One of the powerful tools that can be used is logging. As the code gets complicated, changing an existing codebase or fixing bugs without being able to debug would be quite a nightmare.

The wages of sin is debugging. — Author: Ron Jeffries

In this article, I’ll walk you through how to set up an environment for an SQS triggered lambda function written in Go language. This walkthrough can be used as an example for other triggering mechanisms.

With AWS Serverless Application Model (AWS SAM) CLI, you can easily run or debug AWS Lambda code on your local environment.

Sample Code

As a first step, we need a Lamba Function handler to debug. We’ll use a sample on AWS Lambda Developer Guide. This code piece is a basic handler that consumes SQS events and outputs the message body.

Getting Started

To debug the project on a local environment, AWS SAM CLI & Delve binaries for Linux need to be installed.

Install AWS SAM CLI

brew tap aws/tap
brew install aws-sam-cli

Lambda executes functions on Linux OS so you need Linux Delve debugger binaries for Linux. If the local environment is not a Linux based environment, use the following command to install Delve.

GOOS=linux GOARCH=amd64 go install github.com/go-delve/delve/cmd/dlv@latest

After installing the dependencies, there are 2 files that need to be created under .vscode folder. To pass this step easier, you can use the files in this Git repo.

SAM Specification

AWS SAM uses template.yaml file to define a serverless application. This file also can be used to create AWS resources.

Simply, create a template.yaml file to the project’s root folder. The template defines the Lambda function’s properties, trigger details, and the SQS that the handler will consume.

To run the lambda function on a local machine;

sam local generate-event sqs receive-message — body ‘Test message’ | sam local invoke — event — MySQSQueueFunction

First part of the command creates an SQS message then passes the SQS message payload to local invoke event parameter. You can change event type to other trigger methods eg. SNS, Kinesis or S3. Click for further types.

Or you can use a pre-generated event file as a parameter for sam local invoke command like;

sam local invoke -e sqs_event.json MySQSQueueFunction

VSCode Configuration

We need to run sam local invoke command with -d parameter to attach and debug. Debug argument also needs a port to attachment. Thanks to VSCode tasks.json and launch.json files, we could run preLaunch tasks in the background.

makefile

makefile

Makefile simply defines SAM local invoke command. It can be executed by using the following command.

make debug-with-sam

.vscode/launch.json

Define a debug attachment ports, mode, and preLaunchTask with launch.json file. Before attaching the process, SAM needs to be started in debug mode. preLaunchTask: "debug-with-sam” simply runs the process for us. After running it successfully, it simply attaches itself to the previously started process.

.vscode/launch.json

.vscode/tasks.json

preLaunchTask details need to be defined on tasks.json file. “debug-with-sam” task simply runs make debug-with-sam command that we defined earlier. preLaunchTask details need to be define on tasks.json file. Background tasks need also problem matcher and background configurations to get information about process status.

.vscode/tasks.json

Running

After creating the above-mentioned files, hit the play button on RUN AND DEBUG section. Voila! You’re ready to set breakpoints and hit them.

In case you might need to change sample SQS messages, you can do so by updating makefile file.

Git Repo: https://github.com/hakankutluay/debug-lambda-go

Thank you for reading so far. I hope this will give you a quick introduction to Go Lambda function debugging on VSCode. For more complex use cases, please refer links below.

Thanks soyhan beyazıt for peer reviewing!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store