6 minutes read

POSTED Nov, 2022 dot IN Serverless

MerLoc - Local Debugging for Serverless Applications: Why and How

Serkan Özal

Written by Serkan Özal


Founder and CTO of Thundra

linkedin-share
 X

Merloc2-1

Offering quick deployment, scalability, and a broad range of supported tools, serverless applications are becoming increasingly popular. Flexible pay-as-you-go payment plans offered by most cloud services help minimize costs, adding a financial incentive as the cherry on top of the developer’s cake. But as we know, software engineering is a game of trade-offs, and serverless architectures come with their own challenges.

Debugging Challenges in Serverless Applications

One of the major pain points that we encounter when working with serverless applications is debugging. Traditionally, debugging serverless applications would first require adding log statements around your code; you then have to deploy that code to the testing environment in the cloud services in order to identify the problem.

This approach is both costly and inefficient due to the need to repeatedly and frequently update code and deploy the application. Even running a test is a chargeable service. For example, in AWS Lambda every request costs money, so each request made while attempting to debug adds to your bill. And that’s without taking into account the time it takes while you wait for those changes to be enabled—and as we know, time is money.

Live debugging capabilities in local environments allow you to sidestep these issues, making app development more efficient and less costly. In this article, we’ll explore what live debugging offers for serverless applications, why it matters, and how to implement it using a test case.

What Is Live Debugging

Live debugging means that you are working on an application while it is running. You can interact with the application during the debugging process, which allows you to understand in detail how your application is working. Live debugging also gives you feedback on the state of the application at each stage of development.

Benefits of Live Debugging

Live debugging helps you quickly learn how the application is working in real time and to resolve software issues. For serverless applications, live debugging in a local environment also helps save time and money by freeing you from deploying the application to cloud services after any small change is made. Instead of repeatedly redeploying, you can troubleshoot the problem right away while the application is running.

About MerLoc

MerLoc is an AWS Lambda live debugging tool that allows you to run AWS Lambda functions on your local environment while they are still part of AWS cloud services. MerLoc provides a number of features to help debug applications with ease in your favorite IDE. Its capabilities allow you to:

  • Deploy test functions
  • Debug log statements
  • Debug your application by putting breakpoints in the GUI of IDE
  • Test whether the updated code works by providing hot reload capability
  • Check the integration between the local environment and cloud services without deploying the updated service to the cloud
  • Deploy test function
  • Debug log statement
  • Debug application by putting break-points in the GUI of IDE
  • Allow hot-reload updated functions in local environment automatically to apply changes
  • Run the individual function locally while it is still part of flow shown above and use real requests from the AWS Lambda environment

Demonstration: Building and Debugging an Application

Let’s explore live debugging by building an application and then using MerLoc to debug. I’m a book lover, so I’m going to create an application to manage books in my local library. There are Lambda functions to add new books, update book information, and read the books’ content.

In order to deploy the application to AWS Lambda, you first need to clone the application code from this GitHub repository and then package the application in your local environment using below command:


```
mvn clean package
```

Next, deploy the application to AWS Lambda by running the following command:


```
sls deploy 
```

Note: You need to update the deploymentBucket, region, and profile values in the serverless.yaml file in the project to match with your current AWS S3 bucket, AWS region, and AWS profile in the local environment.

After deploying the application, let’s try to add a new book to the library database using AWS Lambda.

Figure 1: Create a new book from AWS Lambda

We can see that the event has been performed successfully by checking the DynamoDB table (Figure 2). The book Les Miserables has been added.

Now that we’ve successfully deployed the application to AWS Lambda functions, let’s step our application’s functionality up a notch.

Adding New Features to the Application

Books aren’t always permanently available in libraries. Items are sometimes lost by borrowers and not restocked, publishers may fail to provide new supply, and books can even be banned and taken off the shelf. In these instances, we need to be able to remove books from the library. Let’s create a new feature to do exactly that.

The new Lambda function is implemented in function/BookFunctions.java file as follows:


public void deleteBook(String bookId) {

    	log.info("Deleting Book for bookId = " + bookId);
    	BookDao.deleteBook(bookId);
    	log.info("Successfully deleted Book");
	}

Next, let’s deploy the application to the AWS Lambda again and try to delete the book using the bookId.

We’re unable to delete the book using bookId. Let’s click on the “Details” button to get more information about the error.

The error states Could not delete Book, no such bookId, which doesn’t get us very far. We still don’t know the exact reason that we cannot delete the book, which means we can’t begin to fix this problem.

This is where MerLoc comes into play. We can use MerLoc to interactively check the new delete book feature and identify the cause of the error.

Using MerLoc to Debug the Application

In order to use MerLoc to debug the application, we first need to install and set up the MerLoc broker as well as the MerLoc plugin for the integrated development environment (IDE) tool.

Set Up MerLoc to Debug the Application

Make sure you’re installing and setting up the MerLoc broker into your AWS region correctly by following the instructions from thundra-merloc. In short, after cloning the MerLoc repository to your local machine, you need to run the below command to have it install:

AWS_REGION=${your-region} AWS_PROFILE=${your-profile} ./deploy.sh

After installing MerLoc to your AWS region, you need to grab the MERLOC_BROKER_URL which looks like this:

wss://cis8e6auei.execute-api.ap-southeast-1.amazonaws.com/dev.

Save it as a file; we will use this value later on.

The next step is to install the MerLoc plugin for your IDE. Since I’m using IntelliJ to work with my Java project, let’s run through the process for IntelliJ IDEA. First, you need to go to settings, search for the MerLoc plugin, and install it.

Then, add a new configuration for the project using the MerLoc plugin by clicking on the “+” button from “Edit configurations” window.

Set the broker URL and the name in MerLoc’s configurations.

Hit “Apply” and then “OK” to apply the configuration for our project.

The next step is to apply the MerLoc embedded runtime for our module in IntelliJ.

Finally, go to the deleteBook function in AWS Lambda to set up the MerLoc layer and add environment variables for MerLoc. This will integrate MerLoc with AWS Lambda.

Debug the Application from IntelliJ with MerLoc

Let’s investigate the reason that the deleteBook function doesn’t work in detail. First, we need to put debugging points into the code that we want to check.

Next, hit the “debug” button (the bug icon) in order to debug the application.

Go to the AWS function for deleteBook to trigger the debugging process.

We’re now able to debug the function in IntelliJ IDEA. The function stops at the first debugging point for DynamoDBManager initialization code. Click on the small green “next” button on the left side of the debugging panel to move through your debugging points.

We will now go into the deleteBook function in the BookFunction file.

Hit the “next” button to see how it goes.

Here, we see that the request reaches the exception block, which is odd since the bookId we’re using is correct. Taking a closer look we notice the if condition is:

if(!oBook.isPresent()){

}

This line of code is problematic. For some reason, we’re checking if the book does not exist, instead of checking if it does exist.

The correct code is:

if(oBook.isPresent()){

}

Since we’ve discovered the reason the test failed, we can move on to fixing it.

Fix the Bug and Check with Hot Reload

Let’s update DynamoDBBookDao.java with the following code:

@Override

    public String deleteBook(String bookId) {

        Optional<Book> oBook = findBookByBookId(bookId);

        if (oBook.isPresent()) {

            mapper.delete(oBook.get());

        }

        else {

            log.error("Could not delete Book, no such bookId");

            throw new IllegalArgumentException("Delete failed for nonexistent Book");

        }

        return "Success";

    }

Then apply “hot reload” to quickly check the application in your local environment with the help of MerLoc. Click on “Build > Build module” to do this.

Notice the Reloading function environment for line in the console, which means that we’ve successfully applied hot reload to our code changes.

Now let’s go to the deleteBook function and see what happens when we try to delete the book.

The deleteBook function now works as expected. We’re ready to deploy the deleteBook method to AWS Lambda functions.

MerLoc: Improving the Live Debugging Experience

Merloc_-Merloc-architecture-2

Live debugging enables quick resolution of software bugs when developing serverless applications. In addition, it allows you to test new features for the application by giving you access to deep knowledge about how the application works and how different components interact with each otherbefore deploying it to cloud environments. This helps to improve the quality of the application and reduce the cost of cloud services by eliminating the need to repeatedly deploy changes to the cloud environment.

MerLoc, a live AWS Lambda function development tool built to simulate AWS Lambda functions, enables you to deploy the AWS Lambda functions on your local machines with the capability to capture application states while debugging from your favorite IDE. Currently only Java runtime is supported, but Thundra already has plans to support Node.js and Python.

Follow Thundra on GitHub to learn more about how it helps streamline your serverless workflows. Achieve observability across the entire software development lifecycle with MerLoc. Happy debugging!