Domain 3 of 4 · Chapter 2 of 4

Development Environment Testing

Unlock the complete study guide + 1,040 practice questions across 16 full exams.

Bundled into the existing AWS Certified Developer - Associate premium course — no separate purchase.

Included in this chapter:

  • The testing ladder: from in-process unit tests to a deployed test stack
  • Local emulation: the four sam local subcommands and generated events
  • Validate and lint the template before you deploy it
  • Mocking AWS calls, isolating test traffic, and CI execution
  • What SAM local cannot emulate, and exam pattern recognition
ApproachWhat it runsNeeds AWS deploy?CatchesBest for
Unit test (mocked SDK)Handler logic in-process with stubbed AWS clientsNoLogic bugs, branch coverageFast inner loop, no Docker
`sam local invoke` / `start-api`Function in a local Docker Lambda-like containerNoEvent-shape, handler/runtime, route mapping bugsLocal end-to-end before deploy
Deployed test stage / accountReal Lambda + real triggers in an isolated envYes (test stage/account)IAM, integration, latency, quota issuesIntegration tests against the real stack

Decision tree

Did the SAM/CFN template change? Yes sam validate --lint cfn-lint the template before deploy No Test depends on AWS services or triggers? No Unit test (mocked SDK) in-process, no Docker, fast inner loop Yes Need real IAM, latency, or account quotas? No Yes sam local invoke / sam local start-api local Docker container; event-shape, route bugs Deployed test stage / account real Lambda + triggers; isolated from prod Drive with a real payload sam local generate-event

Cheat sheet

  • Test serverless code on a fidelity ladder: unit, then SAM local, then a deployed stage
  • Every sam local subcommand runs the function in Docker, so Docker is a hard prerequisite
  • Use sam local invoke for a one-shot scripted run of a single function
  • Use sam local start-api when the test driver is an HTTP request to your API Gateway routes
  • Use sam local start-lambda when an SDK or the CLI invokes the function, not an HTTP call
  • Generate the test event with sam local generate-event instead of hand-writing the JSON
  • Save event JSON as a named test event in the Lambda console to re-run a deployed function
  • Run sam validate --lint to catch template errors before a deploy rolls back
  • cfn-lint runs standalone against any CloudFormation template, not just SAM ones
  • Mock (stub) the AWS SDK client so unit tests stay fast, offline, and deterministic
  • Isolate deployed test traffic with a separate API Gateway stage or a separate AWS account
  • Use the IAM policy simulator to check whether a policy allows an action without a real request
  • SAM local can't emulate IAM, real latency, or account quotas. Those need a deployed environment
  • Run the test ladder automatically in CodeBuild via buildspec.yml phases
  • Run the X-Ray daemon with -o (local mode) off EC2 to skip the metadata check
  • Point the SDK at a non-default daemon with AWS_XRAY_DAEMON_ADDRESS; bind it with -b for other containers
  • X-Ray annotations are indexed and searchable; metadata is not
  • Set AWS_XRAY_CONTEXT_MISSING to LOG_ERROR to avoid context-missing exceptions outside a request
  • Use sam local -d/--debug-port to attach an IDE debugger to a function
  • Use sam local --warm-containers EAGER to reuse containers between invocations
  • Point the AWS SDK at LocalStack via endpoint_url http://localhost:4566 with placeholder credentials
  • LocalStack does not enforce IAM, so validate permissions against real AWS
  • Cloud9 managed temporary credentials are restricted and disabled when a non-owner shares the environment

Unlock with Premium — includes all practice exams and the complete study guide.

References

  1. Introduction to testing with the sam local command
  2. Intro to sam local invoke
  3. Intro to sam local start-api
  4. Intro to sam local generate-event
  5. sam validate command reference
  6. Validate AWS SAM template files with AWS CloudFormation Linter
  7. Set up stages for a REST API in API Gateway
  8. Intro to sam local start-lambda
  9. Testing Lambda functions in the console
  10. IAM policy testing with the IAM policy simulator
  11. What is AWS CodeBuild?