DEV Community

Cover image for πŸš€ Built a Serverless Image Optimizer with AWS Lambda + S3 (Free Tier Safe)
Kaustav Dey
Kaustav Dey

Posted on

πŸš€ Built a Serverless Image Optimizer with AWS Lambda + S3 (Free Tier Safe)

TL;DR: Upload an image to S3 β†’ Lambda compresses it β†’ Optimized version appears.
Fully serverless, beginner-friendly, and no surprise AWS bills.

πŸ‘‹ Introduction

Image compression is a common but repetitive task β€” especially for indie developers or small teams deploying images to the cloud. So I decided to build an automated image optimizer using AWS Lambda and S3.

This project runs 100% on the AWS Free Tier, and I’ve included teardown scripts to help you avoid any unwanted charges.

Whether you’re learning AWS, building portfolio projects, or automating real-world workflows β€” this one’s for you.

βš™οΈ What I Built

Whenever you upload an image to a specific S3 bucket, it automatically triggers a Lambda function which:

  • Reads the image
  • Compresses it using Pillow (Python image library)
  • Saves an optimized version back to the bucket with the prefix optimized-

This is all done in a serverless fashion β€” no EC2, no containers, no headache.

πŸ› οΈ Tech Stack

  • AWS Lambda β€” Python 3.11 function to handle the image compression
  • Amazon S3 β€” Triggers Lambda on image uploads
  • IAM Roles β€” To grant minimal and secure permissions
  • Pillow β€” For optimizing images in Python
  • Shell Scripts β€” To deploy and clean up resources easily

πŸ“ Project Structure

serverless-image-optimizer/
β”œβ”€β”€ lambda/
β”‚   └── handler.py             # Main Lambda code
β”œβ”€β”€ deploy/
β”‚   β”œβ”€β”€ create_resources.sh    # Setup script
β”‚   β”œβ”€β”€ delete_resources.sh    # Teardown script
β”‚   └── trust-policy.json      # IAM trust policy
β”œβ”€β”€ test-images/               # Sample images
β”œβ”€β”€ requirements.txt           # Pillow dependency
└── README.md
Enter fullscreen mode Exit fullscreen mode

πŸš€ How It Works

  • Upload an image (.jpg, .png) to your S3 bucket.
  • Lambda automatically gets triggered.
  • It compresses and saves the new image as optimized-filename.jpg.
  • Simple. Fast. Scalable. Free-tier friendly.

🧯 Avoiding AWS Charges

Cloud costs can creep up if you’re not careful. This project is:

  • βœ… 100% AWS Free Tier compatible
  • βœ… No long-running services (like EC2)
  • βœ… Includes a delete_resources.sh teardown script

Run this when you’re done:

bash deploy/delete_resources.sh

πŸ“‚ GitHub Repo

πŸ‘‰ View Full Code on GitHub
Includes full setup, teardown, and README documentation.

πŸ“ˆ Who Should Use or Learn from This?

  • Cloud beginners looking to practice Lambda + S3 triggers
  • Indie hackers automating small tasks
  • DevOps learners building their portfolio
  • Freelancers wanting cost-safe infrastructure automation

🧠 What I Learned

  • How to securely connect Lambda to S3
  • Optimizing Lambda deployment with zipped packages
  • Using IAM roles with least-privilege principle
  • Keeping projects within the Free Tier to avoid billing issues

πŸ’¬ Feedback?

Have suggestions? Want to collaborate?
I’m open to feedback, pull requests, or just geeking out over serverless ideas!

πŸ“’ Let’s Connect

If you’re a solo developer, early-stage startup, or indie hacker looking to automate your AWS infra, I’m offering free 30-min consultations.
DM me on LinkedIn or leave a comment!

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.