A complete serverless image resizing solution built with AWS Lambda, S3, and SAM (Serverless Application Model). This application automatically resizes images uploaded to an S3 bucket to 300x300 pixels while maintaining aspect ratio.
Repository: https://github.com/Copubah/serverless-image-resizer
Source S3 Bucket -> S3 Event Notification -> Lambda Function -> Destination S3 Bucket
|
v
CloudWatch Logs & Alarms
|
v
Dead Letter Queue
- Automatic Processing: Images are processed automatically when uploaded to the source S3 bucket
- Multiple Formats: Supports JPG, JPEG, PNG, GIF, BMP, and TIFF formats
- Aspect Ratio Preservation: Maintains original aspect ratio while resizing
- Error Handling: Comprehensive error handling with Dead Letter Queue for failed processing
- Monitoring: CloudWatch alarms for errors and duration monitoring
- Security: IAM roles with least privilege access
- Scalability: Serverless architecture with configurable concurrency limits
serverless-image-resizer/
├── src/
│ └── app.py # Lambda function code
├── events/
│ └── s3-event.json # Test event for local testing
├── template.yaml # SAM template
├── requirements.txt # Python dependencies
├── Makefile # Build and deployment automation
└── README.md # This file
- AWS CLI configured with appropriate permissions
- SAM CLI installed
- Python 3.11
- Docker (for containerized builds)
# macOS
brew install aws-sam-cli
# Windows
choco install aws-sam-cli
# Linux
pip install aws-sam-cligit clone https://github.com/Copubah/serverless-image-resizer.git
cd serverless-image-resizermake installmake validatemake build# Guided deployment (first time)
make deploy-guided
# Or direct deployment
make deploy ENVIRONMENT=dev# Create a test image
make create-test-image
# Upload test image to source bucket
make test-upload
# Check logs
make logsThe Lambda function uses the following environment variables:
DESTINATION_BUCKET: Target S3 bucket for resized imagesTARGET_WIDTH: Target width in pixels (default: 300)TARGET_HEIGHT: Target height in pixels (default: 300)ENVIRONMENT: Deployment environment (dev/staging/prod)LOG_LEVEL: Logging level (INFO/DEBUG/ERROR)
You can customize the deployment using SAM parameters:
sam deploy --parameter-overrides \
Environment=prod \
SourceBucketName=my-source-bucket \
DestinationBucketName=my-dest-bucketUpload images to the source S3 bucket:
aws s3 cp my-image.jpg s3://image-resizer-source-dev-123456789012/Resized images will appear in the destination bucket under the resized/ prefix:
aws s3 ls s3://image-resizer-dest-dev-123456789012/resized/View Lambda logs:
make logsCheck CloudWatch metrics and alarms in the AWS Console.
# Test with sample event
make test-local
# Or use SAM CLI directly
sam local invoke ImageResizerFunction --event events/s3-event.jsonIf you want to add an API Gateway endpoint for direct uploads:
sam local start-apiThe template includes two CloudWatch alarms:
- Error Alarm: Triggers when function errors exceed 5 in 10 minutes
- Duration Alarm: Triggers when average duration exceeds 25 seconds
Failed processing attempts are sent to an SQS Dead Letter Queue for investigation.
All processing activities are logged to CloudWatch Logs with structured logging.
- Unsupported Format: Only image formats in
SUPPORTED_FORMATSare processed - Large Images: Very large images may timeout; consider increasing Lambda timeout
- Permissions: Ensure Lambda has read access to source bucket and write access to destination bucket
- Reserved Concurrency: Limited to 10 concurrent executions to control costs
- Memory Allocation: 512MB provides good balance of performance and cost
- Log Retention: CloudWatch logs retained for 14 days
- S3 Storage Classes: Consider using S3 Intelligent Tiering for cost optimization
- Least Privilege IAM: Lambda only has necessary S3 permissions
- Bucket Policies: Both buckets block public access
- Encryption: Consider enabling S3 bucket encryption
- VPC: For additional security, deploy Lambda in VPC
-
DynamoDB Metadata Tracking:
MetadataTable: Type: AWS::DynamoDB::Table Properties: TableName: !Sub 'image-metadata-${Environment}' AttributeDefinitions: - AttributeName: image_id AttributeType: S KeySchema: - AttributeName: image_id KeyType: HASH
-
Multiple Resize Dimensions:
- Add support for multiple target sizes
- Generate thumbnails, medium, and large versions
-
Image Optimization:
- WebP format conversion for better compression
- Progressive JPEG encoding
- Metadata stripping for privacy
-
API Gateway Integration:
- Direct upload endpoint
- Presigned URL generation
- Progress tracking
-
SNS Notifications:
- Email notifications on processing completion
- Webhook integration for external systems
make deleteIf needed, manually delete:
- S3 buckets and their contents
- CloudWatch log groups
- SQS Dead Letter Queue
| Command | Description |
|---|---|
make help |
Show available commands |
make install |
Install Python dependencies |
make validate |
Validate SAM template |
make build |
Build the application |
make deploy |
Deploy to AWS |
make deploy-guided |
Deploy with guided prompts |
make test-local |
Test function locally |
make test-upload |
Upload test image |
make logs |
Tail Lambda logs |
make describe |
Show stack outputs |
make clean |
Clean build artifacts |
make delete |
Delete the stack |
make create-test-image |
Create test image |
make setup-deployment-bucket |
Create SAM deployment bucket |
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
For issues and questions:
- Check the troubleshooting section
- Review CloudWatch logs
- Check AWS documentation
- Open an issue at: https://github.com/Copubah/serverless-image-resizer/issues