π Build 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
π 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.
πΈ Demo Output
Input Image Optimized Output sample.jpg
optimized-sample.jpg
Compression rate depends on image quality β usually around 40β60% savings.
π§― 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!
π Tags
#aws
#se
rverless
#
lambda
#
python
#devops
#cloud
#s3
#portfolio
#automation
#free-tier
Subscribe to my newsletter
Read articles from Kaustav Dey directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
