A scalable serverless image upload, storage, and retrieval service built with FastAPI, AWS services, and LocalStack for local development.
- Image Upload: Support for JPEG, PNG, GIF, WebP formats with metadata
- Image Listing: Paginated listing with filtering by user, tags, and date
- Image Retrieval: Get individual images with presigned download URLs
- Image Deletion: Soft delete with user authorization
- Scalable Architecture: Built on AWS serverless services
- Local Development: Complete LocalStack setup for offline development
- Comprehensive Testing: Unit tests with high coverage
- API Documentation: Interactive Swagger/ReDoc documentation
- API Gateway: HTTP API entry point
- Lambda Functions: Serverless compute with FastAPI
- S3: Object storage for images
- DynamoDB: NoSQL database for metadata with GSIs for efficient querying
- LocalStack: Local AWS service emulation
- Python 3.7+
- Docker and Docker Compose
- AWS CLI (for LocalStack)
cd infrastructure
docker-compose up -d
- API Base URL: http://localhost:8000
- Interactive Docs: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- Health Check: http://localhost:8000/health
curl -X POST "http://localhost:8000/images" \
-H "Content-Type: multipart/form-data" \
-F "file=@path/to/image.jpg" \
-F "user_id=user123" \
-F "title=Beautiful Sunset" \
-F "description=A stunning sunset over the mountains" \
-F "tags=nature,sunset,mountains"
# List all images
curl "http://localhost:8000/images"
# List with filters
curl "http://localhost:8000/images?user_id=user123&tags=nature&limit=10"
# List user images
curl "http://localhost:8000/users/user123/images"
# List by tag
curl "http://localhost:8000/tags/nature/images"
curl "http://localhost:8000/tags/nature/images"
curl "http://localhost:8000/images/{image_id}/download"
curl -X DELETE "http://localhost:8000/images/{image_id}" \
-H "X-User-Id: user123"
pytest
pytest --cov=src --cov-report=html
# Unit tests only
pytest tests/test_utils/ tests/test_services/
# Integration tests
pytest tests/test_handlers/
# Specific test file
pytest tests/test_services/test_image_service.py -v
docker-compose up -d
export AWS_ACCESS_KEY_ID=test
export AWS_SECRET_ACCESS_KEY=test
export AWS_DEFAULT_REGION=us-east-1
export AWS_ENDPOINT_URL=http://localhost:4566
export S3_BUCKET_NAME=instagram-images-dev
export DYNAMODB_TABLE_NAME=ImageMetadata-dev
uvicorn src.main:app --reload --host 0.0.0.0 --port 8000
# Format code
black src/ tests/
# Lint code
flake8 src/ tests/
# Type checking
mypy src/
scalable_serverless_image_upload/
βββ src/
β βββ handlers/ # Individual Lambda handlers
β βββ services/ # Business logic
β βββ models/ # Pydantic data models
β βββ utils/ # Utility classes
β βββ main.py # Main FastAPI application
βββ tests/ # Comprehensive test suite
βββ infrastructure/ # LocalStack and AWS setup
βββ docs/ # API documentation
βββ requirements.txt # Python dependencies
βββ pytest.ini # Test configuration
βββ README.md # This file
Variable | Default | Description |
---|---|---|
AWS_ACCESS_KEY_ID |
test |
AWS access key (LocalStack) |
AWS_SECRET_ACCESS_KEY |
test |
AWS secret key (LocalStack) |
AWS_DEFAULT_REGION |
us-east-1 |
AWS region |
AWS_ENDPOINT_URL |
http://localhost:4566 |
LocalStack endpoint |
S3_BUCKET_NAME |
instagram-images-dev |
S3 bucket name |
DYNAMODB_TABLE_NAME |
ImageMetadata-dev |
DynamoDB table name |
- Formats: JPEG, PNG, GIF, WebP
- Size: 1KB - 10MB
- Dimensions: 50x50 - 4000x4000 pixels
- Metadata: Title (200 chars), Description (1000 chars), Tags (10 max, 50 chars each)
- Package the application:
pip install -t package/ -r requirements.txt
cp -r src/ package/
cd package && zip -r ../deployment.zip . && cd ..
- Deploy with AWS CLI:
aws lambda create-function \
--function-name instagram-image-service \
--runtime python3.9 \
--role arn:aws:iam::account:role/lambda-role \
--handler src.main.handler \
--zip-file fileb://deployment.zip
- Configure API Gateway:
- Create HTTP API
- Add Lambda integration
- Configure routes:
ANY /{proxy+}
npm install -g serverless
sls deploy
- Logs: Application logs via Python logging
- Metrics: Basic FastAPI metrics
- Health:
/health
endpoint
- AWS CloudWatch: Lambda logs and metrics
- AWS X-Ray: Distributed tracing
- Custom Metrics: Business metrics via CloudWatch
- Alarms: Error rate and latency monitoring
- Input validation and sanitization
- File type and size restrictions
- User authorization for delete operations
- Presigned URLs for secure downloads
- Authentication: JWT or OAuth integration
- Rate Limiting: Per-user and IP-based limits
- CORS: Proper origin configuration
- Encryption: S3 encryption at rest
- WAF: Web Application Firewall
- Secrets: AWS Secrets Manager for credentials
Detailed API documentation is available at:
- Interactive Docs: http://localhost:8000/docs
- Static Docs: docs/api-documentation.md
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
This project is licensed under the MIT License.
For issues and questions:
- Check the troubleshooting section
- Review existing issues
- Create a new issue with detailed information