Skip to content
WritingArticle
  • #aws
  • #sam
  • #serverless
  • #lambda
  • #devops

Test and Debug AWS Serverless Apps Locally with AWS SAM CLI

Pawan Joshi

3 min read

Share
Cover image for “Test and Debug AWS Serverless Apps Locally with AWS SAM CLI”

Clicking around the AWS Console is fine for your very first Lambda function, but it falls apart fast: every tiny code fix means re-uploading a ZIP file and waiting to see CloudWatch logs.

In Week 4 of the 4-Week AWS Serverless Roadmap, you switch to AWS SAM (Serverless Application Model) so you can test, debug, and deploy from your terminal.

Local serverless development loop with AWS SAM CLIThe 4-step SAM CLI loop: local invoke, local API server, deploy, and live log tailing.


Prerequisites

Install:

  1. Docker (SAM CLI runs your Lambda functions inside local AWS-matching Lambda containers)
  2. AWS CLI (configured with aws configure)
  3. AWS SAM CLI (sam --version to verify)

1. sam local invoke — Test One Function Fast

Instead of deploying to AWS to test a single handler, generate a realistic AWS event payload and invoke the function locally:

# Generate a sample API Gateway or S3 event
sam local generate-event apigateway http-api-proxy > event.json
 
# Build and run the function in Docker
sam build
sam local invoke ShortenFunction -e event.json

Why this is great:

  • It runs in the exact Node.js / Python runtime AWS uses in production.
  • You get instant console.log / print() output right in your terminal.
  • Zero AWS charges while iterating.

2. sam local start-api — Run API Gateway on localhost:3000

Want to test your frontend against your backend locally?

sam local start-api --port 3000

SAM reads your template.yaml, mounts every HttpApi / Api route on http://127.0.0.1:3000, and hot-reloads when you edit your function code.

curl -X POST http://127.0.0.1:3000/shorten \
  -H "Content-Type: application/json" \
  -d '{"url":"https://pawanjoshi.dev"}'

Tip for DynamoDB locally: If your Lambda talks to DynamoDB, pass --env-vars env.json to point at a dev table in AWS (using your local AWS CLI credentials) or DynamoDB Local in Docker.


3. sam sync --watch — Instant Cloud Sync for Dev Stacks

Sometimes you need real AWS services (EventBridge, S3 triggers, IAM roles) that are awkward to mock locally. Instead of a slow full CloudFormation deploy every time:

sam sync --watch --stack-name url-shortener-dev

Whenever you save a file, SAM pushes just the updated code/assets to your dev stack in seconds. (Use this only for dev stacks, never production.)


4. sam logs --tail — Read CloudWatch Logs in Your Terminal

Stop clicking through 5 screens in the CloudWatch console to find a stack trace. Tail live logs directly from your terminal:

sam logs -n ShortenFunction --stack-name url-shortener-prod --tail

You can also filter by time or error keyword:

sam logs -n ShortenFunction --stack-name url-shortener-prod --start-time '10min ago' --filter "ERROR"

Quick Cheat Sheet

CommandWhen to use it
sam validate --lintCatch template.yaml syntax/IAM mistakes before deploying
sam buildPackage dependencies and prepare artifacts
sam local invoke Fn -e event.jsonUnit-test one Lambda with a mock event
sam local start-apiTest HTTP routes and CORS locally on localhost:3000
sam deploy --guidedFirst-time CloudFormation deployment
sam logs -n Fn --stack-name s --tailStream live CloudWatch logs in terminal

Found this useful?

Share it with your group