Local Testing & Debugging
Why Test Locally?
- Faster iteration (no AWS roundtrip)
- Cheaper (no invocation charges)
- Better debugging (full IDE features)
- Test edge cases before deploying
Simple Explanation
What it is
Local testing lets you run a serverless function on your laptop before you deploy it to the cloud.
Why we need it
It is faster to catch bugs locally than to deploy, wait, and debug in production.
Benefits
- Quick feedback during development.
- Safer changes because you can test edge cases.
- Lower cost by reducing cloud invocations.
Tradeoffs
- Local environments are not perfect replicas of cloud behavior.
- Tooling setup adds a little upfront time.
Real-world examples (architecture only)
- Local HTTP request → Function → Log output.
- Local event file → Function → Verify response.
Option 1: SAM CLI (Recommended)
SAM (Serverless Application Model) is AWS's framework for local serverless development.
Install SAM
macOS:
brew tap aws/tap
brew install aws-sam-cli
Linux/Windows: Installation Guide
Verify Installation
sam --version
You should see: SAM CLI 1.x.x
Create a Local Project
sam init --runtime python3.12 --package-type Zip
Follow prompts. This creates a template project.
Project Structure
Run Locally
sam local start-api
You'll see:
Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
Test it:
curl http://127.0.0.1:3000/hello
Debug with Breakpoints
sam local start-api --debug
Then attach your IDE's debugger (VS Code, PyCharm, etc.).
Option 2: AWS Toolkit for VS Code
Install Toolkit
- Open VS Code Extensions
- Search: "AWS Toolkit"
- Install by Amazon Web Services
Local Testing
- Open an existing Lambda function in VS Code
- Right-click the handler function
- Select "AWS: Run/Debug Locally"
- Choose "Run" or "Debug"
Toolkit emulates Lambda locally without needing SAM setup.
Option 3: Functions Framework (GCP)
Lightweight local runner for Google Cloud Functions.
Install
python -m pip install functions-framework
Run Locally
functions-framework --target=hello --port=8080
Test it:
curl http://localhost:8080/
Debugging Tips
1. Add Logging
print("Starting function...")
print(f"Event: {event}")
print("Processing complete")
View logs in CloudWatch (AWS) or terminal (local).
2. Use Debugger
Python:
import pdb; pdb.set_trace() # Execution pauses here
3. Test Edge Cases
# Test with empty event
sam local invoke MyFunction -e events/empty.json
# Test with error scenario
sam local invoke MyFunction -e events/error.json
# Test with timeout
sam local invoke MyFunction --env-vars env.json
Common Issues & Solutions
| Issue | Solution |
|---|---|
| "Port 3000 already in use" | sam local start-api --port 3001 |
| Function times out locally | Increase timeout in template.yaml |
| IAM permission errors | Use real AWS credentials (export AWS_PROFILE) |
| Module not found | Ensure dependencies are in requirements.txt and installed |
Best Practices
- Test before deploying — Catch bugs locally
- Use events files — Save test scenarios
- Mock external services — Don't call real DynamoDB during testing
- Version your tests — Test new features before release
- Monitor logs — Both local and CloudWatch
Hands-On: Debug Your HelloServerless Function
Step 1: Install SAM
sam --version
Step 2: Create a test event
{
"name": "Debug Test"
}
Step 3: Run locally
sam local invoke HelloServerless -e test-event.json
Step 4: Add logging
def lambda_handler(event, context):
print("Starting handler")
print(f"Event: {event}")
name = event.get("name", "World")
print(f"Name: {name}")
response = {"message": f"Hello, {name}!"}
print(f"Response: {response}")
return response
Step 5: Test again
sam local invoke HelloServerless -e test-event.json
You should see all logs in your terminal.
Key Takeaway
Local testing makes you a faster, more confident developer. Test everything locally before paying to run it in AWS.
Project (Cloud-Agnostic)
Set up local testing for a Python serverless function and document the steps.
Deliverables:
- Show how to run the function locally.
- Provide at least two test events.
- Describe how you would debug a failing invocation.
If you want feedback, email your write-up to [email protected].
References
- AWS SAM CLI: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli.html
- AWS SAM CLI Install: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html
- Google Cloud Functions Local Development: https://cloud.google.com/functions/docs/running