A serverless AWS Lambda function that automatically resizes images uploaded to an S3 bucket using the sharp image processing library.
- ✅ Resizes images to specified dimensions
- ☁️ Triggered by S3 upload events
- 🧠 Uses the
sharp
library for fast processing - 🪶 Stores the resized image in a target S3 bucket
git clone https://github.com/dlauck92/lambda-image-resizer.git
cd lambda-image-resizer
npm install
Create a policy that allows access to the source and destination S3 buckets.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::YOUR_SOURCE_BUCKET_NAME/*",
"arn:aws:s3:::YOUR_DEST_BUCKET_NAME/*"
]
},
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
}
]
}
aws iam create-policy \
--policy-name ResizeImageS3Policy \
--policy-document file://resize-image-s3-policy.json
Create a trust policy for Lambda:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
aws iam create-role \
--role-name LambdaImageResizerRole \
--assume-role-policy-document file://trust-policy.json
aws iam attach-role-policy \
--role-name LambdaImageResizerRole \
--policy-arn arn:aws:iam::YOUR_ACCOUNT_ID:policy/ResizeImageS3Policy
Replace YOUR_ACCOUNT_ID
with your actual AWS account ID.
zip -r function.zip .
aws lambda create-function \
--function-name image-resizer \
--runtime nodejs18.x \
--role arn:aws:iam::YOUR_ACCOUNT_ID:role/LambdaImageResizerRole \
--handler index.handler \
--timeout 10 \
--memory-size 512 \
--zip-file fileb://function.zip \
--environment Variables="{DEST_BUCKET=your-destination-bucket,MAX_WIDTH=800,MAX_HEIGHT=800}"
- Go to the S3 Console, select your source bucket.
- Go to Properties > Event Notifications.
- Create a new event:
- Event Name:
image-upload
- Event Type:
PUT
- Prefix: (optional, e.g.,
uploads/
) - Destination: Lambda Function →
image-resizer
- Event Name:
Make sure Lambda has permission to be invoked by S3. AWS usually prompts for this automatically.
Upload an image to your source S3 bucket:
aws s3 cp ./example.jpg s3://your-source-bucket/uploads/
The resized image will appear in your destination bucket.
MIT License © Drew Lauck