How do you test AWS Lambda functions locally?

Instruction: Discuss the methods and tools available for locally testing AWS Lambda functions.

Context: This question is designed to explore the candidate's experience and strategies for local development and testing of AWS Lambda functions, ensuring they can iterate quickly before deployment.

Updated

Official answer

Start with ordinary unit tests for the handler's business logic. Supply representative event/context fixtures and mock external clients at explicit boundaries. Include invalid input, duplicates, partial batches, timeouts and failures; assert outputs and side-effect calls rather than only checking that no exception occurred.

Use AWS SAM CLI to build and invoke the application locally in a compatible container. sam local invoke exercises a function with an event fixture; sam local start-api exposes supported local API routes. Match runtime, architecture, handler path and dependencies, and use the debugger when needed.

Make local tests deliberate about network access. SDK calls can still reach real AWS services when credentials and endpoints are available. Use mocks or controlled test endpoints for isolated runs; do not assume that running inside a local container prevents cloud calls.

Mocks and emulators verify selected behavior, not actual IAM permissions, event-source delivery, concurrency quotas, VPC routes or full cloud-service semantics. They can also diverge from current service APIs. Label that coverage accurately.

Before release, run authorized integration and end-to-end tests in an isolated AWS environment using real triggers, policies and configuration. Verify replay/failure handling and business outcomes, then clean up test resources. A passing local test is useful evidence, but not proof that the deployed system is wired correctly.

AWS testing guidance, SAM local commands

Related Questions