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.
The 4-step SAM CLI loop: local invoke, local API server, deploy, and live log tailing.
Prerequisites
Install:
- Docker (SAM CLI runs your Lambda functions inside local AWS-matching Lambda containers)
- AWS CLI (configured with
aws configure) - AWS SAM CLI (
sam --versionto 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.jsonWhy 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 3000SAM 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.jsonto 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-devWhenever 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 --tailYou 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
| Command | When to use it |
|---|---|
sam validate --lint | Catch template.yaml syntax/IAM mistakes before deploying |
sam build | Package dependencies and prepare artifacts |
sam local invoke Fn -e event.json | Unit-test one Lambda with a mock event |
sam local start-api | Test HTTP routes and CORS locally on localhost:3000 |
sam deploy --guided | First-time CloudFormation deployment |
sam logs -n Fn --stack-name s --tail | Stream live CloudWatch logs in terminal |
Found this useful?
Share it with your group