Effortless Debugging: AWS CDK TypeScript Projects in VSCode

Listen to this Post

Debugging AWS Lambda functions can be challenging due to their serverless nature, but two effective approaches include using the Serverless Application Model (SAM) “local” command and the Lambda Live Debugger (currently supporting JavaScript/TypeScript).

You Should Know:

1. Setting Up AWS SAM for Local Debugging

AWS SAM CLI allows you to locally test and debug Lambda functions before deploying them.

Install AWS SAM CLI:

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

Initialize a SAM Project:

sam init --runtime nodejs14.x --name my-lambda-app 

Start Local API for Debugging:

sam local start-api --debug-port 5858 

Configure VSCode for Debugging:

Add this to your `launch.json`:

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

2. Using Lambda Live Debugger

For JavaScript/TypeScript functions, AWS now supports Lambda Live Debugging directly in VSCode.

Enable Live Debugging in AWS Console:

  1. Navigate to Lambda > Functions > YourFunction > Monitor > Live Debugging.

2. Follow the setup steps to connect VSCode.

Configure VSCode for Live Debugging:

{
"version": "0.2.0",
"configurations": [
{
"type": "aws-lambda",
"request": "attach",
"name": "Attach to Lambda",
"lambda": {
"functionName": "your-function-name",
"invocationEvent": {}
}
}
]
}

3. Debugging with AWS CDK

AWS CDK allows infrastructure-as-code debugging.

Install CDK:

npm install -g aws-cdk 

Debug CDK Stack:

cdk synth --debug 
cdk deploy --debug 

Use Breakpoints in CDK Code:

Set breakpoints in `lib/your-stack.ts` and run:

cdk deploy --debug --no-rollback 

What Undercode Say:

Debugging serverless functions doesn’t have to be painful. Using SAM CLI and Lambda Live Debugger, developers can efficiently troubleshoot AWS Lambda functions. For CDK users, integrating VSCode debugging ensures smooth infrastructure debugging.

Additional Useful Commands:

  • Test Lambda Locally:
    sam local invoke "YourFunction" -e event.json 
    
  • View Lambda Logs:
    aws logs tail /aws/lambda/YourFunction --follow 
    
  • List AWS Lambda Functions:
    aws lambda list-functions 
    
  • Update Lambda Code:
    aws lambda update-function-code --function-name YourFunction --zip-file fileb://deploy.zip 
    

Expected Output:

A fully configured VSCode debugging environment for AWS Lambda, with breakpoints, live logs, and seamless CDK deployment debugging.

Reference:

Effortless Debugging: AWS CDK TypeScript Projects in VSCode

References:

Reported By: Darryl Ruggles – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 TelegramFeatured Image