Testing Lambda Handlers in AWS¶
Quick guide for deploying and testing Lambda handler changes in real AWS, isolated from existing infrastructure.
How It Works¶
Three scripts create an isolated set of Lambda functions with a unique prefix (e.g., pr230). Resources created:
| Resource | Naming | Example |
|---|---|---|
| IAM Role | tradai-<prefix>-lambda-role | tradai-pr230-lambda-role |
| Lambda Functions | tradai-<prefix>-<handler> | tradai-pr230-promote-model |
| ECR Tags | <prefix> on existing repos | tradai/lambda-promote-model:pr230 |
| CloudWatch Logs | /aws/lambda/tradai-<prefix>-* | Auto-created by Lambda |
Existing dev/prod resources are never modified.
Prerequisites¶
- AWS CLI installed and credentials set (env vars or
aws configure) - Docker running
- UV installed (
justcommands use it)
Quick Start¶
# 1. Login via AWS SSO (opens browser, lasts 8-12 hours)
aws sso login --profile tradai
# 2. Setup — builds images, pushes to ECR, creates Lambda functions
bash scripts/test-infra-setup.sh pr230
# 3. Test — invokes each Lambda with test payloads, generates report
bash scripts/test-infra-test.sh pr230
# 4. Review report
cat scripts/output/test-infra-pr230-report.md
# 5. Cleanup — deletes all created resources
bash scripts/test-infra-cleanup.sh pr230
What Each Script Does¶
test-infra-setup.sh <prefix>¶
- Builds Docker images for 5 Lambda handlers (linux/amd64)
- Pushes to existing ECR repos with tag
<prefix>(notlatest) - Creates IAM role with basic Lambda + DynamoDB read permissions
- Creates Lambda functions using the pushed images
- Saves state to
scripts/output/test-infra-<prefix>-state.json
Duration: ~10-15 min (first run, includes Docker builds)
test-infra-test.sh <prefix>¶
- Invokes each Lambda with realistic payloads
- Checks responses for errors
- PASS = handler loaded and ran (even if AWS SDK errors occur — expected without VPC)
- FAIL = import errors, syntax errors, unexpected crashes
- Generates Markdown report in
scripts/output/
test-infra-cleanup.sh <prefix>¶
- Deletes all Lambda functions with prefix
- Deletes CloudWatch Log Groups
- Detaches policies and deletes IAM role
- Removes ECR image tags
- Verifies nothing remains
Customizing Handlers¶
Edit the HANDLERS array in test-infra-setup.sh to test different handlers:
HANDLERS=(
check-retraining-needed
cleanup-resources
model-rollback
promote-model
retraining-scheduler
# Add more:
# health-check
# drift-monitor
# backtest-consumer
)
Limitations¶
- Lambda functions run without VPC access — they cannot reach DynamoDB, ECS, MLflow, or other AWS services in the private VPC.
- AWS SDK calls will fail with
EndpointConnectionErrororClientError— this is expected and proves the handler loads correctly up to that point. - For full integration testing (real DynamoDB/MLflow calls), use the full
just infra-bootstrapdeployment.
Cost¶
- Lambda: Free tier covers 1M requests/month. Test invocations: ~10 = $0.00
- ECR: ~$0.10/GB/month for stored images. 5 images ≈ 1-2 GB = ~$0.20/month
- CloudWatch Logs: Negligible for test runs
- IAM Role: Free
Total: effectively $0 for test runs. Cleanup removes all billable resources.
Troubleshooting¶
"aws: command not found"¶
# Windows (Chocolatey)
choco install awscli
# Or add to PATH manually
export PATH="$PATH:/c/Program Files/Amazon/AWSCLIV2"
"ExpiredToken"¶
Session tokens (from SSO/assume-role) expire after 1-12 hours. Refresh credentials and re-export env vars.
"Docker build fails"¶
Ensure Docker Desktop is running and has linux/amd64 platform support:
"ECR push denied"¶
ECR login may have expired (12-hour token):