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.
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.
The app consists of the following components:
- API Gateway: Handles user requests and forwards them to Lambda.
- Lambda Function: Processes the uploaded images.
- S3 Buckets:
- Source Bucket stores the original uploaded images.
- Destination Bucket stores the processed images.
- Log in to the AWS Management Console.
- Ensure your AWS account has the necessary permissions to work with S3, Lambda, API Gateway, and IAM.
- Navigate to the S3 service in the AWS Management Console.
- Create two buckets:
- Source Bucket:
image-upload-bucket
- Destination Bucket:
processed-images-bucket
- Configure both buckets:
- Enable block public access for security.
- Optionally enable versioning for file management.
- Navigate to the IAM service.
- Create a new role:
- Trusted entity: AWS Lambda.
- Attach the following policies:
- AmazonS3FullAccess: Grants full access to S3 buckets.
- CloudWatchLogsFullAccess: Allows logging for debugging.
- Save the role with a descriptive name, e.g.,
LambdaS3ProcessingRole
.
- Navigate to AWS Lambda.
- 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
).
Install Dependencies
Install the Pillow library and package it with your Lambda function code:
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:
The following code resizes images before saving them in the destination bucket:
You can use a Lambda layer to include the
Pillow
library, so you don't need to include it in every deployment:- Create a Layer with Pillow: Follow the steps above to install
Pillow
into a directory, but skip adding your code. Package only the library.
- 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).
- Attach the Layer to Your Function:
- Go to your Lambda function.
- Under Layers, click Add a layer.
- Select the layer you created.
- Test the Function: Test your Lambda function again. The
Pillow
library should now be available.
- Navigate to the Source Bucket in the S3 console.
- Configure an event notification:
- Event Type:
All object create events
. - Destination: Select the Lambda function created earlier.
- Save the event notification.
- Navigate to Amazon API Gateway and create a new REST API.
- Add a resource:
- Path:
/upload
- Enable CORS.
- Create a POST method:
- Integration Type: Lambda Function.
- Select the Lambda function.
- Deploy the API to a new stage (e.g.,
prod
) and copy the Invoke URL.
- 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.
- Verify:
- The uploaded image appears in the Source Bucket.
- The processed image appears in the Destination Bucket.
- Use API Gateway Authorizers (e.g., Cognito) to secure the API endpoint.
- Apply Bucket Policies to restrict access.
- 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! 🚀