Skip to content

Amirtha655/mobile-image-optimizer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⚡ Serverless Image Optimization Pipeline

Automatically compress, resize & convert images to WebP — powered by AWS

AWS SAM Python Lambda S3 License


Drop a 4MB photo in. Get a 80KB WebP out. Automatically. In under a second.



🧩 The Problem

Modern apps are drowning in raw images. Users upload massive smartphone photos — 3 to 8 MB each. Serving those originals to mobile users means:

  • 🐢 Slow load times → higher bounce rates
  • 💸 Expensive bandwidth bills at scale
  • 📱 Terrible experience on 4G/5G networks
  • 🔧 Developers manually compressing assets

✅ The Solution

A fully serverless, event-driven pipeline that intercepts every upload, optimizes it, and stores a mobile-ready version — with zero human involvement.

Upload JPEG/PNG  →  S3 Trigger  →  Lambda (Pillow)  →  Optimized WebP
     4 MB        →   instant    →   resize + convert  →    ~80 KB

🏗️ Architecture

┌─────────────┐     s3:ObjectCreated      ┌──────────────────┐
│   Developer  │ ──── raw-uploads/ ──────▶ │   AWS Lambda     │
│   / App      │                           │   Python 3.13    │
└─────────────┘                           │   + Pillow        │
                                          └────────┬─────────┘
                                                   │
                                          resize + convert
                                          to WebP (quality 80)
                                                   │
                                                   ▼
                                    ┌──────────────────────────┐
                                    │  S3: optimized-delivery/ │
                                    │  filename.webp (~80KB)   │
                                    └──────────────────────────┘
Layer Service Purpose
Storage Amazon S3 Receives uploads, stores output
Compute AWS Lambda Runs image processing code
Trigger S3 Event Notification Fires Lambda on every upload
IaC AWS SAM + CloudFormation Deploys entire stack in one command
Permissions AWS IAM Least-privilege read/write access
Image Engine Pillow (PIL) Resize, convert, compress

✨ Features

  • 🚀 Zero infrastructure — fully serverless, scales automatically
  • 🔁 Event-driven — triggers instantly on every S3 upload
  • 📐 Smart resizing — max 400px width, aspect ratio preserved
  • 🖼️ WebP conversion — 25–34% smaller than JPEG at same quality
  • 🔒 Secure by default — private bucket, least-privilege IAM
  • ♾️ Infinite loop safe — skips already-optimized files
  • 📊 Observable — full CloudWatch logging on every invocation

📁 Project Structure

mobile-image-optimizer/
│
├── template.yaml              # SAM/CloudFormation — all AWS resources
│
├── image_processor/
│   ├── app.py                 # Lambda handler — core processing logic
│   └── requirements.txt       # Python dependencies (Pillow)
│
└── Serverless_Image_Optimization_Pipeline.pdf   # Full technical documentation

🚀 Quick Start

Prerequisites

Deploy in 2 commands

sam build
sam deploy --guided

That's it. SAM provisions the S3 bucket, Lambda function, IAM roles, and event wiring automatically.

Test it

# Upload any image to the raw-uploads/ prefix
aws s3 cp your-photo.jpg s3://<your-bucket>/raw-uploads/your-photo.jpg

# Watch the Lambda logs live
aws logs tail /aws/lambda/sam-app-image-optimizer --follow

Check your S3 bucket for the optimized-delivery/ folder — your .webp file will be there within seconds.


📊 Real Test Results

Metric Before After
File Size ~4,000 KB ~80 KB
Format JPEG / PNG WebP
Width Full resolution Max 400px
Processing Time < 1 second
Human Effort Manual Zero

🔮 Future Enhancements

  • CloudFront CDN for global edge delivery
  • Generate multiple sizes (thumbnail / mobile / desktop)
  • SQS Dead-Letter Queue for failed retries
  • SNS notifications on completion
  • EXIF metadata extraction to DynamoDB

💰 Cost

Nearly free for most workloads.

  • Lambda — first 1M requests/month are free
  • S3 — ~$0.023/GB/month storage
  • Example: 100,000 images/month ≈ under $0.25

📄 Documentation

A full technical PDF is included in this repo covering architecture, IAM design, data flow, test results, and real-world use cases.

👉 Serverless_Image_Optimization_Pipeline.pdf


Built with ❤️ using AWS SAM · Lambda · S3 · Python · Pillow

About

An event-driven, serverless asset pipeline built with AWS SAM and Python Pillow to optimize mobile app image delivery and reduce cloud egress costs.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages