This project provides a complete setup for developing and testing AWS Lambda functions locally using Python.
- Python 3.9 or higher
- AWS CLI (for deployment)
- AWS SAM CLI (for local development)
-
Clone and navigate to the project:
cd lambda-bedrock
-
Install dependencies:
make install # or pip install -r requirements.txt
-
Run local tests:
make test # or python test_lambda.py
lambda-bedrock/
βββ lambda_function.py # Main Lambda function handler
βββ test_lambda.py # Local testing script
βββ test_events.json # Sample test events
βββ requirements.txt # Python dependencies
βββ template.yaml # SAM template for deployment
βββ samconfig.toml # SAM configuration
βββ Makefile # Development commands
βββ .gitignore # Git ignore rules
βββ README.md # This file
python test_lambda.py
This will run the Lambda function with sample events and display the results. The script includes mocked Bedrock responses for testing.
# Run all tests
make test-pytest
# Run specific test categories
make test-unit # Unit tests only
make test-integration # Integration tests only
make test-bedrock # Bedrock-related tests only
# Run tests with coverage
make test-coverage
# Build the function
make local-build
# Start local API Gateway
make local-start
Then test the endpoints:
POST http://localhost:3000/chat/completions
sam local invoke LambdaFunction --event test_events.json
Command | Description |
---|---|
make install |
Install Python dependencies |
make test |
Run local tests with mocked Bedrock |
make test-pytest |
Run pytest test suite |
make test-unit |
Run unit tests only |
make test-integration |
Run integration tests only |
make test-bedrock |
Run Bedrock-related tests only |
make test-coverage |
Run tests with coverage report |
make local-build |
Build Lambda function locally |
make local-start |
Start local API Gateway |
make local-invoke |
Invoke Lambda function locally |
make clean |
Clean build artifacts |
make format |
Format code with black |
make lint |
Lint code with flake8 |
make type-check |
Type check with mypy |
make deploy |
Deploy to AWS |
The Lambda function includes:
- Chat Completions API: OpenAI-compatible chat completions endpoint
- AWS Bedrock Integration: Uses custom Bedrock model for text generation
- Image Processing: Support for image inputs with base64 encoding
- Conversation History: Maintains context across multiple messages
- Error Handling: Comprehensive error handling and logging
- Local Testing: Built-in mock context and Bedrock mocking for local development
- Multiple Response Formats: Handles various Bedrock response structures
curl -X POST http://localhost:3000/chat/completions \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role": "user", "content": "Hello, how are you?"}
],
"model": "custom-model",
"max_tokens": 100,
"temperature": 0.7
}'
Response:
{
"id": "chatcmpl-abc12345",
"object": "chat.completion",
"created": 1672531200,
"model": "custom-model",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! I'm doing well, thank you for asking."
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 8,
"total_tokens": 18
}
}
curl -X POST http://localhost:3000/chat/completions \
-H "Content-Type: application/json" \
-d '{
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "What do you see in this image?"},
{"type": "image_url", "image_url": {"url": "data:image/jpeg;base64,..."}}
]
}
],
"model": "custom-model"
}'
- AWS CLI configured with appropriate credentials
- S3 bucket for deployment artifacts
- AWS SAM CLI installed
- Update samconfig.toml with your S3 bucket name
- Deploy using SAM:
make deploy # or sam deploy --guided
# Build
sam build
# Deploy
sam deploy --guided
- Modify
lambda_function.py
to handle new routes - Add test events to
test_events.json
- Update the SAM template if needed
The function supports these environment variables:
ENVIRONMENT
: Environment name (dev/staging/prod)LOG_LEVEL
: Logging level (INFO/DEBUG/ERROR)
- Add test cases to
test_events.json
- Run
make test
to verify locally - Use
make local-start
to test with API Gateway
The function includes structured logging that works with:
- AWS CloudWatch Logs
- Local development console
- Custom log aggregators
- CORS headers are configured for cross-origin requests
- Input validation should be added for production use
- Environment variables for sensitive configuration
- IAM roles follow least privilege principle
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run
make test
to ensure everything works - Submit a pull request
- Import errors: Ensure all dependencies are installed with
make install
- Port conflicts: Change the port in
make local-start
if 3000 is occupied - AWS credentials: Ensure AWS CLI is configured for deployment
- SAM build errors: Try
make clean
thenmake local-build
- Check the AWS Lambda documentation
- Review the SAM CLI documentation
- Ensure Python version compatibility (3.9+)