Local Testing and Debugging with SAM CLI
Run and debug your serverless functions on your own machine before deploying. Use sam local to invoke functions, emulate API Gateway, and iterate fast.
Local Testing and Debugging with SAM CLI is a free Serverless Backend with AWS Lambda & API Gateway lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Serverless Backend with AWS Lambda & API Gateway learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Test Locally?
Deploying to AWS just to test a change is slow. The SAM CLI emulates Lambda and API Gateway locally using Docker, so you can iterate in seconds.
Prerequisites
Local invocation needs:
- Docker running
- The SAM CLI installed
- A valid
template.yaml
Invoking a Single Function
Run one function with a test event payload.
sam local invoke HelloFunction --event events/event.jsonGenerating Sample Events
SAM can generate realistic event payloads for many AWS sources, saving you from writing them by hand.
sam local generate-event apigateway aws-proxy > events/event.jsonEmulating the API
Start a local HTTP server that mimics API Gateway, routing requests to your functions.
sam local start-api
# then: curl http://localhost:3000/helloEnvironment Variables Locally
Supply env vars via a JSON file so local runs match what the function expects.
sam local invoke HelloFunction \
--env-vars env.jsonHot Reload During Development
SAM watches your source: when you save a change, the next request uses the new code without restarting the local API, keeping the feedback loop tight.
Step Debugging
Attach a debugger by passing a debug port. Your IDE connects to it so you can set breakpoints inside the running container.
sam local invoke HelloFunction -d 5858Validating the Template
Catch template errors before deploying with a quick validation command.
sam validate --lintViewing Logs
After a deploy you can tail CloudWatch logs from the terminal, filtering by function.
sam logs -n HelloFunction --tailLimitations of Local Emulation
Local mode is great for logic, but it cannot perfectly mirror IAM permissions, real network latency, or managed services like DynamoDB. Always run an integration test in a real AWS stage before production.
Quick Check
Test your SAM CLI knowledge.
Recap
You learned local development with SAM:
sam local invokeruns a single functionsam local start-apiemulates API Gateway- Generate events, supply env vars, and attach a debugger
sam validatecatches template errors early- Local mode has limits — verify in a real stage before prod
Frequently asked questions
Is the “Local Testing and Debugging with SAM CLI” lesson free?
Yes — the full text of “Local Testing and Debugging with SAM CLI” is free to read here on the web, and the Serverless Backend with AWS Lambda & API Gateway course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Serverless Backend with AWS Lambda & API Gateway course, upgrade to CoddyKit PRO.
What will I learn in “Local Testing and Debugging with SAM CLI”?
Run and debug your serverless functions on your own machine before deploying. Use sam local to invoke functions, emulate API Gateway, and iterate fast. You practise Serverless Backend with AWS Lambda & API Gateway with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Serverless Backend with AWS Lambda & API Gateway?
No prior experience is required. Serverless Backend with AWS Lambda & API Gateway on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Local Testing and Debugging with SAM CLI” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Serverless Backend with AWS Lambda & API Gateway lesson?
Yes. Every Serverless Backend with AWS Lambda & API Gateway lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to AWS SAM
- Defining Serverless Resources
- Deploying SAM Applications
- Local Testing and Debugging with SAM CLI