0Pricing
Serverless AWS Lambda Development · Lesson

Local Testing with SAM CLI

Learn to invoke, emulate, and debug your serverless application locally with the AWS SAM CLI before deploying to the cloud.

Local Testing with SAM CLI is a free Serverless AWS Lambda Development 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 AWS Lambda Development 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 test every change is slow. The SAM CLI emulates Lambda and API Gateway on your machine for a fast feedback loop.

What You Need

Local emulation runs functions inside Docker containers that mirror the Lambda runtime, so you need Docker installed alongside the SAM CLI.

Invoking a Single Function

Use sam local invoke to run one function once with a test event, just like a single Lambda execution.

sam local invoke OrdersFunction \
  --event events/order.json

A Test Event File

The event file is the JSON payload your handler receives. Keep a folder of sample events for different scenarios.

{
  "body": "{\"item\":\"book\",\"qty\":2}",
  "httpMethod": "POST"
}

Generating Sample Events

SAM can scaffold realistic events for common sources so you do not handcraft them.

sam local generate-event s3 put > events/s3.json
sam local generate-event apigateway aws-proxy > events/api.json

Running a Local API

sam local start-api stands up a local HTTP server that routes requests to your functions, mimicking API Gateway.

sam local start-api --port 3000

Calling the Local API

Once running, hit it with any HTTP client. The response comes from your real handler code.

curl -X POST http://localhost:3000/orders \
  -d '{"item":"book","qty":2}'

Environment Variables Locally

Pass a JSON file of environment variable overrides so local runs point at test resources instead of production.

sam local invoke OrdersFunction \
  --env-vars env.json

Step-Through Debugging

Start with -d to pause for a debugger to attach on a port, letting you set breakpoints in your function code.

sam local invoke OrdersFunction -d 5858

Limits of Local Emulation

Local mode cannot perfectly reproduce IAM, networking, or cold starts. Treat it as a fast first check, not a replacement for a cloud staging test.

Validate the Template

Before deploying, run sam validate to catch template errors early.

sam validate --lint

Quick Check

Test your local-testing knowledge.

Recap

You learned to invoke functions, generate events, run a local API, inject env vars, and attach a debugger with the SAM CLI, plus its emulation limits.

Frequently asked questions

Is the “Local Testing with SAM CLI” lesson free?

Yes — the full text of “Local Testing with SAM CLI” is free to read here on the web, and the Serverless AWS Lambda Development 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 AWS Lambda Development course, upgrade to CoddyKit PRO.

What will I learn in “Local Testing with SAM CLI”?

Learn to invoke, emulate, and debug your serverless application locally with the AWS SAM CLI before deploying to the cloud. You practise Serverless AWS Lambda Development 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 AWS Lambda Development?

No prior experience is required. Serverless AWS Lambda Development 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 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 AWS Lambda Development lesson?

Yes. Every Serverless AWS Lambda Development 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

  1. AWS Serverless Application Model (SAM)
  2. Introduction to Serverless Framework
  3. CI/CD for Serverless Apps
  4. Local Testing with SAM CLI
← Back to Serverless AWS Lambda Development