Debugging serverless applications presents unique challenges, but debugging AWS Lambda functions with AWS SAM provides a powerful solution. The AWS Serverless Application Model (SAM) CLI enables local debugging that replicates the Lambda execution environment, allowing you to troubleshoot issues efficiently without deploying to AWS.

Pro Tip: Debugging locally with SAM can reduce troubleshooting time by up to 70% compared to debugging in the cloud.

Debugging workflow with AWS SAM showing local vs cloud debugging

Why Debug Lambda Functions Locally?

Traditional cloud debugging has significant drawbacks:

  • Slow feedback cycles (deploy → test → repeat)
  • Difficulty reproducing environment-specific issues
  • Limited access to execution context
  • CloudWatch logging delays
  • Accumulated debugging costs

Kid-Friendly Explanation

Imagine trying to fix a robot in space versus fixing it in your workshop. SAM is like bringing the space robot back to your workshop where you have all your tools. You can take your time, use special instruments, and test fixes immediately!

Setting Up Your Debugging Environment

Prerequisites

  • AWS SAM CLI installed (sam --version should show 1.85+)
  • Docker running on your local machine
  • VS Code or your preferred IDE
  • Node.js/Python/Java runtime matching your Lambda

Basic SAM Application Structure

my-sam-app/
├── template.yaml        # SAM configuration
├── src/
│   ├── app.js           # Lambda function code
│   ├── package.json     # Node.js dependencies
├── events/
│   ├── event.json       # Test event payload

Debugging Workflow Step-by-Step

1 Start Debug Session

Initiate debugging with the SAM CLI:

sam local invoke -d 5858 MyFunction 
  -e events/event.json 
  --debug

2 Attach Debugger

Configure your IDE to connect to the debug port (5858). In VS Code:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Attach to SAM CLI",
      "type": "node",
      "request": "attach",
      "address": "localhost",
      "port": 5858,
      "localRoot": "${workspaceFolder}",
      "remoteRoot": "/var/task",
      "protocol": "inspector"
    }
  ]
}

3 Set Breakpoints

Place breakpoints in your code where you need inspection:

Setting breakpoints in VS Code for Lambda debugging

4 Inspect Variables

Use debug console to examine:

  • Event object structure
  • Environment variables
  • Context properties
  • Intermediate values

Advanced Debugging Techniques

Debugging API Gateway Events

Simulate API requests locally:

sam local start-api --debug-port 5858

Environment Variable Management

Replicate production environment variables:

# template.yaml
Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Environment:
        Variables:
          DB_HOST: localhost
          DEBUG_MODE: true

Debugging Layers

Include Lambda layers in local debugging:

sam local invoke --layer-cache ./layers
Pro Tip: Use sam logs -n MyFunction --tail to stream logs after debugging sessions for additional context.

Common Debugging Scenarios

Permissions Issues

Symptoms: “Access Denied” errors in Lambda

Debugging approach:

  1. Check IAM role in SAM template
  2. Verify resource-based policies
  3. Test with minimal permissions

Cold Start Problems

Symptoms: Slow initial execution

Debugging approach:

sam local invoke --debug-port 5858 
  --profile warm

Environment Configuration

Symptoms: Missing environment variables

Debugging approach:

// Print environment variables
console.log(process.env);

VS Code Debugging Configuration

Complete launch.json setup:

{
  "configurations": [
    {
      "name": "Debug Lambda",
      "type": "node",
      "request": "attach",
      "address": "localhost",
      "port": 5858,
      "localRoot": "${workspaceFolder}",
      "remoteRoot": "/var/task",
      "protocol": "inspector",
      "preLaunchTask": "sam-invoke",
      "sourceMaps": true
    }
  ]
}

Download Debugging Cheat Sheet

Get our complete SAM debugging reference guide:

Download Full Guide

Comparing Debugging Methods

MethodSetup TimeExecution SpeedAccuracyBest For
SAM Local DebuggingMediumFastHighComplex logic, pre-deployment
CloudWatch LogsLowSlowMediumPost-deployment analysis
X-Ray TracingHighSlowHighDistributed systems

Best Practices

  • Always debug with the same runtime version as production
  • Use --skip-pull-image after initial setup to speed up debugging
  • Create realistic test event payloads in events/ directory
  • Combine debugging with unit tests for comprehensive coverage
  • Profile memory usage with sam local invoke --profile

Further Reading

Conclusion

Mastering debugging AWS Lambda functions with AWS SAM transforms your serverless development workflow. By leveraging local debugging capabilities, you can identify and resolve issues faster, reduce deployment cycles, and build more reliable serverless applications. Remember to integrate SAM debugging into your regular development process rather than treating it as a last-resort troubleshooting tool.

Final Tip: For complex distributed systems, combine SAM local debugging with AWS X-Ray for end-to-end tracing after deployment.