Kavin Duraisamy

Debugging AWS Lambda Functions: How to Run Locally for Effective Debugging

Local Debugging Challenges for Serverless AWS Applications

During development or troubleshooting, it’s essential to trace the execution of code line by line or at specific breakpoints. This approach allows us to gain insights into the code flow and resolve issues more efficiently. Most traditional applications can be easily run locally, enabling the use of debuggers and breakpoints for tracing the execution flow.

However, serverless applications on AWS do not function like traditional standalone applications, making it challenging to perform local debugging. This can lead to a tedious development process, as changes need to be deployed to the AWS environment each time an issue needs troubleshooting. Fortunately, the AWS Serverless Application Model (SAM) tool simplifies this complexity by enabling local debugging, resulting in faster and more effective troubleshooting in a local environment.

What is AWS SAM?

The AWS Serverless Application Model (AWS SAM) is a toolkit that significantly enhances the developer experience of building and running serverless applications on AWS. It consists of two core components:

  1. AWS SAM Template Specification
  2. AWS SAM Command Line Interface (AWS SAM CLI)

AWS SAM Template Specification

The AWS SAM Template Specification is an open-source framework designed to define the AWS serverless infrastructure. It bears a resemblance to AWS CloudFormation templates and plays a pivotal role in enabling local Lambda function execution. In this template file, you define the configurations necessary for Lambda functions to run seamlessly in a local development environment.

AWS SAM CLI

The AWS SAM CLI is a powerful command-line tool that works in conjunction with AWS SAM templates to facilitate the development and testing of serverless applications. It offers a suite of tools for easily scaffolding serverless functions and running them in your local development environment.

Installing AWS SAM

You can install the AWS SAM tool by visiting the official documentation page at https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html. The page provides binaries and installation instructions for multiple operating systems, including Windows, macOS, and Linux.

Scaffolding the HelloWorld Lambda Function

The AWS SAM toolkit provides a Quick Starter template to simplify the process of creating Lambda functions. Follow these steps to generate a Hello World Lambda function in just a minute:

  1. Run the following init command in your shell:
sam init
  1. When prompted, select “1” for AWS Quick Start Templates and answer the subsequent questions, such as runtime and package type, as needed. This will configure your Hello World Lambda function.

AWS SAM init wizard

Building and Invoking the Lambda Function

To build the Lambda package, use the following commands:

  1. Run the build command:
sam build
  1. Invoke the Lambda function locally using the SAM invoke command:
sam local invoke "HelloWorldFunction" -e events/event.json
  • HelloWorldFunction is the name of the Lambda function.
  • events/event.json contains the event data that triggers the Lambda function. This file is generated by sam init during scaffolding, but you can update it with your own event data as needed.

Enable Debugging

To enable debugging for your Lambda function, follow these steps:

  1. Run the invoke command with the -d option to start the remote debugger on a specified port (e.g., 5858):
sam local invoke -d 5858 "HelloWorldFunction" -e events/event.json
  1. In your integrated development environment (IDE), such as Eclipse, import the function and connect to the remote debugger using port 5858 and the localhost. This will allow you to debug your Lambda function locally. Eclips remote debug configurations
  • Add breakpoints in your code where you want to pause execution and gain insights. Run the debugger to trace the execution and effectively diagnose any issues in your Lambda function.

Eclipse Debugger

Summary

In conclusion, AWS SAM offers a practical solution for debugging and running AWS Lambda functions locally, simplifying the development process and accelerating issue resolution.