Skip to content

lakriz/serverless-image-resizer

 
 

Repository files navigation

Serverless Image Resizer

GitHub License: MIT AWS Python

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

Architecture

Source S3 Bucket -> S3 Event Notification -> Lambda Function -> Destination S3 Bucket
                                                     |
                                                     v
                                            CloudWatch Logs & Alarms
                                                     |
                                                     v
                                              Dead Letter Queue

Features

  • 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

Project Structure

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

Prerequisites

  • AWS CLI configured with appropriate permissions
  • SAM CLI installed
  • Python 3.11
  • Docker (for containerized builds)

Install SAM CLI

# macOS
brew install aws-sam-cli

# Windows
choco install aws-sam-cli

# Linux
pip install aws-sam-cli

Quick Start

1. Clone and Setup

git clone https://github.com/Copubah/serverless-image-resizer.git
cd serverless-image-resizer

2. Install Dependencies

make install

3. Validate Template

make validate

4. Build Application

make build

5. Deploy to AWS

# Guided deployment (first time)
make deploy-guided

# Or direct deployment
make deploy ENVIRONMENT=dev

6. Test the Application

# Create a test image
make create-test-image

# Upload test image to source bucket
make test-upload

# Check logs
make logs

Configuration

Environment Variables

The Lambda function uses the following environment variables:

  • DESTINATION_BUCKET: Target S3 bucket for resized images
  • TARGET_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)

Parameters

You can customize the deployment using SAM parameters:

sam deploy --parameter-overrides \
  Environment=prod \
  SourceBucketName=my-source-bucket \
  DestinationBucketName=my-dest-bucket

Usage

Upload Images

Upload images to the source S3 bucket:

aws s3 cp my-image.jpg s3://image-resizer-source-dev-123456789012/

Check Results

Resized images will appear in the destination bucket under the resized/ prefix:

aws s3 ls s3://image-resizer-dest-dev-123456789012/resized/

Monitor Processing

View Lambda logs:

make logs

Check CloudWatch metrics and alarms in the AWS Console.

Local Development

Test Locally

# Test with sample event
make test-local

# Or use SAM CLI directly
sam local invoke ImageResizerFunction --event events/s3-event.json

Local API (Optional)

If you want to add an API Gateway endpoint for direct uploads:

sam local start-api

Monitoring and Troubleshooting

CloudWatch Alarms

The template includes two CloudWatch alarms:

  1. Error Alarm: Triggers when function errors exceed 5 in 10 minutes
  2. Duration Alarm: Triggers when average duration exceeds 25 seconds

Dead Letter Queue

Failed processing attempts are sent to an SQS Dead Letter Queue for investigation.

Logs

All processing activities are logged to CloudWatch Logs with structured logging.

Common Issues

  1. Unsupported Format: Only image formats in SUPPORTED_FORMATS are processed
  2. Large Images: Very large images may timeout; consider increasing Lambda timeout
  3. Permissions: Ensure Lambda has read access to source bucket and write access to destination bucket

Cost Optimization

  • 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

Security Best Practices

  • 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

Enhancements

Suggested Improvements

  1. 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
  2. Multiple Resize Dimensions:

    • Add support for multiple target sizes
    • Generate thumbnails, medium, and large versions
  3. Image Optimization:

    • WebP format conversion for better compression
    • Progressive JPEG encoding
    • Metadata stripping for privacy
  4. API Gateway Integration:

    • Direct upload endpoint
    • Presigned URL generation
    • Progress tracking
  5. SNS Notifications:

    • Email notifications on processing completion
    • Webhook integration for external systems

Cleanup

Delete Stack

make delete

Manual Cleanup

If needed, manually delete:

  1. S3 buckets and their contents
  2. CloudWatch log groups
  3. SQS Dead Letter Queue

Makefile Commands

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

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

For issues and questions:

  1. Check the troubleshooting section
  2. Review CloudWatch logs
  3. Check AWS documentation
  4. Open an issue at: https://github.com/Copubah/serverless-image-resizer/issues

Repository

About

Complete serverless image resizing solution with AWS Lambda, S3, and SAM. Automatically resizes images to 300x300 pixels with error handling, monitoring, and DynamoDB metadata tracking.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Python 63.3%
  • Shell 29.0%
  • Makefile 7.7%