Image Processing on the Fly using Sharp and AWS Lambda
← All projectsA quick demonstration of serverless computing and automated image processing using AWS Lambda and Amazon S3.
It shows an event-driven architecture where uploading a standard image to an S3 bucket automatically triggers a Node.js Lambda function. The function uses the Sharp image processing library to generate three outputs at the same time:
- A standard 50% resized thumbnail (the base requirement)
- A grayscale version (bonus technique 1)
- A color-inverted (negate) version (bonus technique 2)
The video proves the code ran successfully, that the S3 folders are generated properly, and that infinite invocation loops are avoided.
Overview
A small serverless pipeline: upload an image to cloud storage and three processed versions appear by themselves, made by a Lambda function.
The problem
Resizing and converting images by hand, or on a server that runs all day, wastes time and money for work that only happens when someone uploads.
What I built
- Upload triggers a Node.js Lambda function automatically
- A 50% thumbnail, a grayscale version and a colour-inverted version, made at the same time with Sharp
- Output written to its own folder in the bucket
- Guarded against infinite invocation loops
Architecture
An upload to an S3 bucket fires an event that runs a Node.js Lambda function. The function processes the image with Sharp and writes three results to a separate output prefix that does not trigger the function again.
A high-level view. Endpoints, hosts and security details are left out on purpose.
Key decisions
- 01
Event-driven, not always on
Why: The function runs only when an image arrives, so there is no idle server to pay for.
Trade-off: The first run after a quiet period starts a little slower.
- 02
Outputs go to a separate prefix
Why: Writing results where they don't re-trigger the function avoids an endless loop and a surprise bill.
Trade-off: The trigger and folder layout must be set up carefully.
Links
The link is the video walkthrough. The source code is private; I'm happy to walk you through it.