How can I test lambda in local using python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The fastest way to test an AWS Lambda function locally in Python is to separate business logic from the Lambda handler and run ordinary unit tests against that logic. After that, use a local invocation tool such as AWS SAM when you need to verify the full event shape and runtime behavior. That gives you both fast feedback and realistic integration coverage.
Start with a Thin Handler
A Lambda handler is easier to test when it mostly translates the incoming event into a normal function call. Keep parsing, validation, and response creation close to the handler, and move business rules into plain Python functions.
This structure keeps most of your code independent from the Lambda runtime. That matters because plain Python functions are much easier to test than a full serverless deployment package.
Unit Test the Handler with pytest
You can call the handler directly and pass a small fake context object. For many Lambda functions, this covers the most important logic.
Run the tests locally:
This is the best default workflow because it is fast and does not require Docker or a deployed AWS stack.
Test Real Event Shapes with AWS SAM
When you want to verify that your handler works with API Gateway, EventBridge, or S3 event payloads, use AWS SAM. SAM emulates the Lambda runtime more closely than a plain unit test.
A minimal event file for an API Gateway style request might look like this:
Invoke the function locally:
If your Lambda is fronted by an API, you can also run:
That lets you send normal HTTP requests to a local endpoint while your Python handler executes in a Lambda-like environment.
Mock AWS Services Instead of Calling Real Ones
If your function talks to S3, DynamoDB, or SNS, avoid hitting real AWS services during most local tests. Mock them so your tests stay fast, deterministic, and cheap.
This style is useful when your Lambda includes AWS SDK calls but you still want tests that run without network access.
Keep Test Inputs Close to Production Events
Local Lambda tests become much more valuable when the sample events resemble real traffic. Save representative payloads from API Gateway, SQS, or EventBridge and use them in tests and local invocations. That catches mistakes in field names and nested structures early.
Common Pitfalls
- Writing all business logic directly inside
lambda_handler, which makes tests harder to isolate. - Relying only on deployed-cloud tests. They are slower and harder to debug than local unit tests.
- Using fake events that are much simpler than production payloads.
- Calling real AWS services during unit tests when a mock would be enough.
- Forgetting to define environment variables locally when the Lambda depends on configuration.
Summary
- Keep the handler thin and move logic into plain Python functions.
- Use
pytestfor fast local unit tests. - Use AWS SAM when you need realistic event and runtime behavior.
- Mock AWS services for deterministic local tests.
- Reuse production-like sample events so local failures match real issues more closely.

