AWS Logo
Menu
Serverless App to Resize & add Watermarks on images

Serverless App to Resize & add Watermarks on images

serverless image upload and processing application using S3, API Gateway, and Lambda

Published Dec 10, 2024
Last Modified Dec 23, 2024
we’ll create a serverless image upload and processing application using AWS services like S3, API Gateway, and Lambda. This app allows users to upload images, process them (e.g., resizing or adding watermarks), and store the processed images in a different S3 bucket.

Why Use AWS for Image Upload and Processing?

AWS offers scalable and reliable services for handling image upload and processing workflows:
  • Amazon S3: For secure and durable storage.
  • AWS Lambda: For serverless, on-demand image processing.
  • Amazon API Gateway: For exposing a secure API endpoint for image uploads.

Project Architecture

The app consists of the following components:
  1. API Gateway: Handles user requests and forwards them to Lambda.
  2. Lambda Function: Processes the uploaded images.
  3. S3 Buckets:
    • Source Bucket stores the original uploaded images.
    • Destination Bucket stores the processed images.

Step-by-Step Implementation

Step 1: Set Up Your AWS Environment

  1. Log in to the AWS Management Console.
  2. Ensure your AWS account has the necessary permissions to work with S3, Lambda, API Gateway, and IAM.

Step 2: Create S3 Buckets

  1. Navigate to the S3 service in the AWS Management Console.
  2. Create two buckets:
    • Source Bucket: image-upload-bucket
    • Destination Bucket: processed-images-bucket
  3. Configure both buckets:
    • Enable block public access for security.
    • Optionally enable versioning for file management.

Step 3: Create an IAM Role for Lambda

  1. Navigate to the IAM service.
  2. Create a new role:
    • Trusted entity: AWS Lambda.
  3. Attach the following policies:
    • AmazonS3FullAccess: Grants full access to S3 buckets.
    • CloudWatchLogsFullAccess: Allows logging for debugging.
  4. Save the role with a descriptive name, e.g., LambdaS3ProcessingRole.

Step 4: Create a Lambda Function

  1. Navigate to AWS Lambda.
  2. Create a new function:
    • Name: ImageProcessingFunction
    • Runtime: Python 3.x (or Node.js, depending on your preference).
    • Execution Role: Select the IAM role created earlier(LambdaS3ProcessingRole).

Code: Processing Images with Pillow (Python)

Install Dependencies
Install the Pillow library and package it with your Lambda function code:
Code Snippet
The following code resizes images before saving them in the destination bucket:

Alternative Solution: Use a Lambda Layer

You can use a Lambda layer to include the Pillow library, so you don't need to include it in every deployment:
  1. Create a Layer with Pillow: Follow the steps above to install Pillow into a directory, but skip adding your code. Package only the library.
  1. Upload the Layer:
    • Go to the AWS Lambda console.
    • Navigate to the Layers section.
    • Create a new layer, upload the pillow_layer.zip, and specify the runtime (e.g., Python 3.8 or 3.9).
  2. Attach the Layer to Your Function:
    • Go to your Lambda function.
    • Under Layers, click Add a layer.
    • Select the layer you created.
  3. Test the Function: Test your Lambda function again. The Pillow library should now be available.

Step 5: Set Up Event Notification for S3

  1. Navigate to the Source Bucket in the S3 console.
  2. Configure an event notification:
    • Event Type: All object create events.
    • Destination: Select the Lambda function created earlier.
  3. Save the event notification.

Step 6: Configure API Gateway

  1. Navigate to Amazon API Gateway and create a new REST API.
  2. Add a resource:
    • Path: /upload
    • Enable CORS.
  3. Create a POST method:
    • Integration Type: Lambda Function.
    • Select the Lambda function.
  4. Deploy the API to a new stage (e.g., prod) and copy the Invoke URL.

Step 8: Testing the Application

  1. Use tools like Postman or cURL to test the API:
    • Endpoint: https://your-api-id.execute-api.region.amazonaws.com/prod/upload
    • Method: POST
    • Body: Base64-encoded image data.
  2. Verify:
    • The uploaded image appears in the Source Bucket.
    • The processed image appears in the Destination Bucket.

OPtional: Secure Your Application

  1. Use API Gateway Authorizers (e.g., Cognito) to secure the API endpoint.
  2. Apply Bucket Policies to restrict access.
  3. Enable CloudWatch Logs to monitor and debug your application.
-----------------------------------------------------------------------------------------------------------
Congratulations! You’ve successfully built an image upload and processing application using AWS. With this architecture:
  • Users can upload images seamlessly.
  • Images are processed efficiently using AWS Lambda.
  • Processed files are stored securely in an S3 bucket.
This project demonstrates the power of serverless computing and the flexibility of AWS services. You can extend this application further by:
  • Adding more advanced image processing features.
  • Integrating a front-end application for a complete user interface.
Happy Cloud Learning! 🚀
 

Comments