S3 Image Thumbnail Generator With Lambda
Part of the AWS Roadmap
In short
Upload a photo, get a thumbnail a moment later, with no server running all day. This classic project ties S3 events, Lambda, packaging and IAM into one small, satisfying pipeline.
An event-driven project where uploading an image to one S3 bucket triggers a Lambda function that creates a thumbnail and saves it to another bucket. It shows S3 events, Lambda layers or packaging, and IAM permissions.
Think of it like this
The photo studio's passport-size counter
At a studio in Kochi, you drop off a big print in one tray. A worker notices, makes a small passport-size copy and puts it in a second tray. He never touches the second tray's photos again, or he'd keep shrinking his own work. The upload bucket is the first tray, Lambda is the worker, and the thumbnails bucket is the second tray.
Watch it happen
From upload to thumbnail
1/5A user uploads holi-party.jpg to the photos-upload bucket.
The big ideas
What you need to know
Event notifications
Configure the source bucket to invoke the function on s3:ObjectCreated:*, filtered by suffix such as .jpg.
Packaging Pillow
Pillow has compiled parts, so build it for Lambda's Linux environment, as a layer, in a zip built for the right architecture, or as a container image.
Least privilege
The role needs s3:GetObject on the source bucket, s3:PutObject on the thumbnails bucket and permission to write logs. Nothing more.
Avoid loops
Never write the output where it triggers the same function again. Use two buckets or strict prefix filters.
See it in code
Walk through the code
1import io2import os3from urllib.parse import unquote_plus4 5import boto36from PIL import Image7 8s3 = boto3.client("s3")9THUMB_BUCKET = os.environ["THUMB_BUCKET"]10 11def handler(event, context):12 for record in event["Records"]:13 bucket = record["s3"]["bucket"]["name"]14 key = unquote_plus(record["s3"]["object"]["key"])15 16 obj = s3.get_object(Bucket=bucket, Key=key)17 img = Image.open(io.BytesIO(obj["Body"].read()))18 img.thumbnail((300, 300))19 20 out = io.BytesIO()21 img.convert("RGB").save(out, format="JPEG", quality=85)22 out.seek(0)23 s3.put_object(Bucket=THUMB_BUCKET, Key=f"thumbs/{key}",24 Body=out, ContentType="image/jpeg")1/4Create the client once outside the handler so warm invocations reuse it.
Python on Lambda with boto3 and Pillow. THUMB_BUCKET is set as an environment variable so the same code works in dev and prod.
Save yourself the pain
Common mistakes
Oops
Using the raw object key from the event, so files with spaces fail with NoSuchKey.
Do this instead
Decode it with unquote_plus before calling get_object.
Oops
Installing Pillow with pip on a Mac or Windows laptop and zipping it.
Do this instead
Those wheels won't run on Lambda's Linux. Build with sam build --use-container, use matching manylinux wheels, or use a container image.
Your mission
Build the thumbnailer
Quick check
Why use a separate bucket for thumbnails instead of the same bucket?
Go deeper
- 1Use two separate buckets to avoid the function triggering itself in a loop.
- 2Package the Pillow library correctly for the Lambda runtime.
- 3Log processing time and errors to CloudWatch and add an alarm for failures.
More projects at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.