Debug AWS Lambda functions with Thundra Online Debugging

Serverless Guru
Mar 16 · 7 min read
Image for post
Image for post

Today we are going to be talking about a cool offering from the folks over at Thundra called, Online Debugging. They’ve built something that is pretty interesting and I think everyone working in the serverless space will be excited to experiment with it.

High level summary

  1. Setup a Thundra account
  2. Thundra onboarding
  3. Build our AWS Lambda function
  4. Setup the AWS Lambda function
  5. Setup the Thundra VSCode plugin
  6. Online debugging
  7. Final Thoughts

Set up a Thundra account

The first thing we need to do is sign up for a Thundra account.

Image for post
Image for post
Sign Up

Then we can log in and get rolling.

Image for post
Image for post
Log In

Thundra onboarding

There are two paths when getting set up with Thundra, the pre-existing option is the Thundra Console and the new addition that this article is covering is the Thundra Debugger. Select Thundra Debugger.

Image for post
Image for post
Select “Start Debug”

Now we are prompted with an authentication key and a series of steps we need to follow to get the VSCode plugin setup.

Image for post
Image for post
NodeJS + VSCode integration

The first step is to install the Thundra Node.js agent which we are going to walkthrough below.

Build the AWS Lambda function

Typically an AWS Lambda function only requires a handful of files, especially when working with NodeJS.

Below we are creating our initial files and installing the @thundra/core npm dependency.

$: mkdir debugging
$: cd debugging
$: npm init -y
$: npm install --save @thundra/core
$: touch index.js

Now we can write our index.js file which will hold our business logic.

// index.js
const thundra = require("@thundra/core")();
exports.handler = thundra(async event => {
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
});

At the top, you can see that we are importing the @thundra/core dependency and then wrapping our exports.handler function code with the @thundra/core dependency. This wrapper code will connect all of our AWS Lambda executions back to the Thundra platform.

Once we have our function code updated, we need to zip the required AWS Lambda files. Then we can move to the AWS console.

$: zip function.zip index.js package.json node_modules -r

Setup the AWS Lambda function

Now that we have our AWS Lambda zip file ready to upload. We can navigate to the AWS Lambda console and create a function.

Image for post
Image for post
Create AWS Lambda function

Now we need to configure some details such as naming (thundraTest), setting up our runtime (NodeJS 12.x), and our AWS IAM role (lambda_basic_execution).

Image for post
Image for post
AWS Lambda create function settings

Once our AWS Lambda function is created we need to configure two environment variables:

  1. thundra_apiKey — during onboarding choose “Thundra Console” and you should see this value (optional)
  2. thundra_agent_lambda_debugger_auth_token — during onboarding choose “Thundra Debugging” and you should see this value
Image for post
Image for post
Set your Thundra API Key

Now it’s time to upload our AWS Lambda zip file from our previous steps.

Image for post
Image for post
AWS Lambda upload zip file

Once the zip file is uploaded we can now test our function.

Image for post
Image for post
AWS Lambda create test

Once we hit Test we should see the following result.

Image for post
Image for post
AWS Lambda test result

Once you have the AWS Lambda function successfully returning a 200 response. We then need to set up the VSCode plugin.

Setup the Thundra VSCode plugin

The first thing we need to do is simply install the Thundra VSCode plugin.

Image for post
Image for post
Thundra Debugger VS Code plugin

Next, we need to create a local file to authenticate our VSCode with our Thundra account.

$: touch debug-client.json

Now we need to set up the file by replacing <set-your-thundra-auth-token> with our Thundra auth token.

{
"profiles": {
"default": {
"debugger": {
"authToken": <set-your-thundra-auth-token>,
"sessionName": "default",
"brokerHost": "debug.thundra.io",
"brokerPort": 443
}
}
}
}

Now that the VSCode plugin is set up locally. We need to add another environment variable, thundra_agent_lambda_debugger_auth_token, to our deployed AWS Lambda function.

Image for post
Image for post

This extra environment variable will now cause our AWS Lambda function to connect to our VSCode environment.

Let’s take a look at using Thundra online debugging.

Online debugging with VSCode

Online debugging allows a developer to set breakpoints on live deployed AWS Lambda functions. To get this setup we need to walk through a couple of steps.

  1. Place breakpoints in your local code
  2. Run the Thundra: Start Debugger command
  3. Run your AWS Lambda function in the AWS Console

After the AWS Lambda function executes, you can step through each breakpoint and see the variables side-by-side.

First off we need to set breakpoints in our code. I’m just setting the breakpoint underneath the event.

Image for post
Image for post
Online debugging breakpoint

Then we need to start the debugger inside VSCode.

Image for post
Image for post
Online debugging start debugger

Then we need to switch back to the AWS console and invoke our AWS Lambda function.

Image for post
Image for post
Online debugging test

Make sure that you set the timeout pretty high on the function that you’re debugging. When you run the AWS Lambda function and it connects to VSCode it’s only open for a limited window. That window is whatever you set the AWS Lambda timeout too. If you set the timeout to 30 seconds, then you can only play around with the VSCode Online debugger for 30 seconds before it breaks. Based on the size of your AWS Lambda function you might want to have a timeout of a few minutes if you need additional time to process what values are being populated.

And once the connection is made, we should be able to step through our breakpoints one-by-one and debug those values.

Image for post
Image for post
Online debugging result

As you can see above the side panel is populated with variables from our live AWS Lambda function and you can see the event object had our test event we made previously when setting up the AWS Lambda function in the AWS console.

Final Thoughts

Overall I think the Thundra Online debugger is a powerful solution for developers looking to avoid emulating the cloud locally. By using the VSCode plugin you are utilizing real AWS resources and you’re able to debug a live deployed AWS Lambda function via breakpoints without needing to redeploy any code.

If you have any questions on the content covered, please comment below.

Next Steps

If you want to learn more about serverless, we’d highly recommend you check out more of our articles or other content.

Why Serverless Guru?

Serverless Guru exists to be a change agent and overall guide to companies around the globe who are moving to serverless at scale. We help companies understand where serverless fits and where it doesn’t. Then we lay a proven roadmap to move them along in their serverless journey whether they are just getting started or already developing production serverless applications. If you’re looking for top-grade serverless consultants, send us an email at info@serverlessguru.com and we can schedule a quick call.

What did we miss?

When you leave your answer make sure to either comment below or tweet your answer to @serverlessgurux on Twitter because then we can quickly get back to you!

Image for post
Image for post

Ryan Jones

Founder, CEO/CTO — Serverless Guru

LinkedIn — @ryanjonesirl

Twitter — @ryanjonesirl

Thanks for reading 😃

If you would like to learn more about Serverless Guru, please follow us on Medium, Twitter, Instagram, Facebook, or LinkedIn!

Serverless Guru

Serverless guides on your cloud journey

Serverless Guru

Written by

We can help you migrate to serverless, build serverless applications, and train your team on serverless best practices. https://www.serverlessguru.com

Serverless Guru

Serverless Guru exists to be a change agent and overall guide to companies around the globe whom are moving to serverless at scale. We help companies understand where serverless fits and where it doesn’t. Then we lay a proven roadmap to move them along in their serverless journey

Serverless Guru

Written by

We can help you migrate to serverless, build serverless applications, and train your team on serverless best practices. https://www.serverlessguru.com

Serverless Guru

Serverless Guru exists to be a change agent and overall guide to companies around the globe whom are moving to serverless at scale. We help companies understand where serverless fits and where it doesn’t. Then we lay a proven roadmap to move them along in their serverless journey

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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