AWS Builder Center

Building a Serverless Image Resizer with AWS Lambda and Amazon S3

Learn how to set up an automated, event-driven image resizing pipeline on AWS using S3 event notifications and a Lambda function in under 10 minutes.

CREATIVE DESINGER | SOFTWARE DEVLOPER
Introduction Processing media uploads efficiently is a common challenge in modern web application development. Offloading image optimization to a serverless workflow ensures your application scales seamlessly without needing dedicated server infrastructure.
In this tutorial, you will build an automated image processing pipeline using Amazon S3 and AWS Lambda.
Prerequisites Before getting started, make sure you have:
An active AWS Account.
Basic knowledge of AWS Lambda and Amazon S3.
The AWS CLI installed and configured (optional, but recommended).
Architecture Overview A user uploads an image to an S3 Source Bucket.
S3 triggers an Event Notification upon file upload.
An AWS Lambda function runs, fetches the uploaded image, resizes it, and saves it to a Destination S3 Bucket.
Step-by-Step Implementation
  1. Create S3 Buckets Create two separate S3 buckets in the AWS Management Console:
my-app-original-images (Source)
my-app-resized-images (Destination)
  1. Create the Lambda Function Open the AWS Lambda Console and click Create function.
Choose Author from scratch, set the runtime to Node.js or Python, and name your function ImageResizerFunction.
Assign an IAM role that grants s3:GetObject permissions for the source bucket and s3:PutObject for the destination bucket.
  1. Add Image Processing Code (Python Example) Python import os import urllib.parse import boto3 from PIL import Image
s3_client = boto3.client('s3')
def lambda_handler(event, context): # Retrieve bucket and key from event source_bucket = event['Records'][0]['s3']['bucket']['name'] key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8') dest_bucket = "my-app-resized-images"
# Download file to /tmp directory
download_path = f"/tmp/{key}"
upload_path = f"/tmp/resized-{key}"
s3_client.download_file(source_bucket, key, download_path)

# Resize image
with Image.open(download_path) as image:
image.thumbnail((300, 300))
image.save(upload_path)
# Upload to destination bucket
s3_client.upload_file(upload_path, dest_bucket, f"resized-{key}")
return {"statusCode": 200, "body": "Image resized successfully"}
4. Configure S3 Event Notification Navigate back to your source bucket in the S3 Console.
Go to Properties > Event notifications > Create event notification.
Select All object create events (s3:ObjectCreated:*).
Set the destination to your ImageResizerFunction Lambda function.
Testing Upload a .jpg or .png file into your source bucket. Check your destination bucket after a few seconds to see the newly generated thumbnail.
Clean-up To avoid unexpected charges:
Empty and delete both S3 buckets.
Delete the Lambda function and its associated IAM role.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article