Repository: https://github.com/Copubah/serverless-image-resizer
serverless-image-resizer/
├── src/
│ └── app.py # Main Lambda function for image resizing
├── events/
│ └── s3-event.json # Test S3 event for local testing
├── scripts/
│ ├── deploy.sh # Automated deployment script
│ ├── test.sh # Comprehensive testing script
│ └── cleanup.sh # Resource cleanup script
├── enhancements/
│ ├── template-with-dynamodb.yaml # Enhanced SAM template with DynamoDB
│ ├── app-with-dynamodb.py # Enhanced Lambda with metadata tracking
│ └── metadata_api.py # API for querying image metadata
├── template.yaml # Main SAM template
├── requirements.txt # Python dependencies
├── samconfig.toml # SAM configuration
├── Makefile # Build and deployment automation
├── .gitignore # Git ignore patterns
├── README.md # Comprehensive documentation
└── PROJECT_STRUCTURE.md # This file
- Purpose: Main Lambda function for image resizing
- Features:
- Processes S3 events automatically
- Resizes images to 300x300 pixels maintaining aspect ratio
- Supports JPG, JPEG, PNG, GIF, BMP, TIFF formats
- Comprehensive error handling and logging
- Uploads resized images to destination bucket
- Purpose: AWS SAM template defining infrastructure
- Resources:
- Source and destination S3 buckets
- Lambda function with proper IAM permissions
- S3 event notifications
- CloudWatch alarms and log groups
- SQS Dead Letter Queue
- Environment-specific parameters
- Purpose: Python dependencies
- Dependencies:
boto3==1.34.0- AWS SDKPillow==10.1.0- Image processing library
- Purpose: SAM CLI configuration
- Features:
- Environment-specific deployment settings
- Build and deployment parameters
- Multi-environment support (dev/staging/prod)
- Purpose: Build and deployment automation
- Commands:
make install- Install dependenciesmake validate- Validate SAM templatemake build- Build applicationmake deploy- Deploy to AWSmake test-upload- Upload test imagemake logs- View Lambda logsmake clean- Clean build artifactsmake delete- Delete stack
- Purpose: Automated deployment with validation
- Features:
- Prerequisites checking (AWS CLI, SAM CLI, credentials)
- Deployment bucket creation
- Template validation
- Containerized build
- Stack deployment with outputs
- Purpose: Comprehensive testing automation
- Features:
- Test image creation
- Image upload to source bucket
- Processing verification
- Dimension validation
- Local invocation testing
- Performance metrics collection
- Purpose: Complete resource cleanup
- Features:
- S3 bucket emptying
- Stack deletion with confirmation
- Local artifact cleanup
- Optional deployment bucket cleanup
- Purpose: Sample S3 event for local testing
- Usage: Used with
sam local invokefor testing
- Purpose: Enhanced SAM template with additional features
- Additional Resources:
- DynamoDB table for metadata tracking
- API Gateway for metadata queries
- SNS topic for notifications
- Additional CloudWatch alarms
- Enhanced monitoring and observability
- Purpose: Enhanced Lambda function with metadata tracking
- Additional Features:
- DynamoDB metadata storage
- SNS notifications
- Compression ratio calculation
- Processing time tracking
- Enhanced error handling
- Purpose: REST API for querying image metadata
- Endpoints:
GET /images- List all processed imagesGET /images/{image_id}- Get specific image metadata
- Features:
- Pagination support
- Filtering by date and source bucket
- Computed metrics (processing duration, size reduction)
- Stack Name:
serverless-image-resizer-dev - Buckets:
- Source:
image-resizer-source-dev-{account-id} - Destination:
image-resizer-dest-dev-{account-id}
- Source:
- Function:
image-resizer-dev
- Stack Name:
serverless-image-resizer-staging - Buckets:
- Source:
image-resizer-source-staging-{account-id} - Destination:
image-resizer-dest-staging-{account-id}
- Source:
- Function:
image-resizer-staging
- Stack Name:
serverless-image-resizer-prod - Buckets:
- Source:
image-resizer-source-prod-{account-id} - Destination:
image-resizer-dest-prod-{account-id}
- Source:
- Function:
image-resizer-prod
All resources follow a consistent naming pattern:
- Format:
{service}-{component}-{environment}-{account-id} - Examples:
- S3 Buckets:
image-resizer-source-dev-123456789012 - Lambda Functions:
image-resizer-dev - DynamoDB Tables:
image-metadata-dev - CloudWatch Alarms:
image-resizer-errors-dev
- S3 Buckets:
- Principle of Least Privilege: Lambda functions have minimal required permissions
- Resource-Specific: Permissions scoped to specific buckets and tables
- Environment Isolation: Each environment has separate resources and permissions
- Public Access Blocked: All buckets have public access blocked
- Versioning Enabled: Both source and destination buckets have versioning
- Encryption: Server-side encryption enabled (can be enhanced with KMS)
- VPC Deployment: Can be deployed in VPC for additional isolation
- Security Groups: Configurable for VPC deployments
- Private Subnets: Lambda can be deployed in private subnets
- Lambda Metrics: Duration, errors, invocations, throttles
- S3 Metrics: Object counts, bucket sizes
- DynamoDB Metrics: Read/write capacity, throttling
- Error Alarm: Triggers on function errors > 5 in 10 minutes
- Duration Alarm: Triggers on average duration > 25 seconds
- DynamoDB Throttle Alarm: Triggers on throttling events
- Structured Logging: JSON-formatted logs with correlation IDs
- Log Retention: 14-day retention for cost optimization
- Log Levels: Configurable (INFO, DEBUG, ERROR)
- Memory: 512MB for optimal price/performance
- Timeout: 30 seconds to prevent runaway costs
- Reserved Concurrency: Limited to 10 to control costs
- Lifecycle Policies: Can be added for automatic archiving
- Intelligent Tiering: Recommended for cost optimization
- Compression: Images are optimized during processing
- On-Demand Billing: Pay-per-request pricing
- Point-in-Time Recovery: Enabled for data protection
- Auto Scaling: Automatic capacity management
- Lambda Concurrency: Configurable reserved concurrency
- S3 Event Processing: Automatically scales with upload volume
- DynamoDB: On-demand scaling for variable workloads
- Container Reuse: Lambda containers are reused for efficiency
- Connection Pooling: AWS SDK connections are reused
- Image Processing: Optimized Pillow settings for performance
- Multiple Resize Dimensions: Support for multiple target sizes
- WebP Conversion: Modern format support for better compression
- Batch Processing: Process multiple images in single invocation
- CDN Integration: CloudFront distribution for resized images
- Machine Learning: Automatic image tagging and categorization
- API Gateway: Direct upload endpoints with presigned URLs
- EventBridge: Integration with other AWS services
- Step Functions: Complex image processing workflows
- SES: Email notifications with processing reports