This example demonstrates a robust serverless architecture on AWS for handling file uploads and automated thumbnail generation. It leverages AWS SAM (Serverless Application Model), Lambda functions, API Gateway, and S3 event notifications to create a streamlined and scalable image processing pipeline.


Project Structure

Here’s a quick overview of the project’s key components:

  • functions/: Contains the core Lambda function code:
    • event.mjs: Handles S3 event notifications, triggering the thumbnail generation process.
    • upload.mjs: Manages the initial file upload through API Gateway.
  • layers/nodejs/: A Lambda layer containing shared code and dependencies for the functions:
    • lib/utils.mjs: Common utility functions.
    • package.json & package-lock.json: Node.js dependencies for the layer.
  • template.yaml: The AWS SAM template defining the serverless resources: Lambda functions, API Gateway endpoints, S3 buckets, and their respective permissions and event triggers.

How It Works

The workflow is straightforward and efficient:

  1. File Upload: Users upload files via an API Gateway endpoint, which triggers the upload.mjs Lambda function. This function securely uploads the file to an S3 bucket.
  2. S3 Event Notification: Once a file is successfully uploaded to the S3 bucket, an S3 Event Notification is triggered.
  3. Thumbnail Generation: This S3 event invokes the event.mjs Lambda function. This function then processes the newly uploaded image, generates a thumbnail, and stores it in a separate S3 bucket or a designated folder within the same bucket.

This architecture provides a decoupled and scalable solution for image processing, where uploads and thumbnail generation are handled independently, ensuring high availability and efficient resource utilization.


Project Repository: https://github.com/OmarMakled/aws-sam-s3-event