Serverless Image Resizing with AWS Lambda, Python & Boto3

Updated on May 15,2025

Table of Contents

In today's cloud-driven world, efficient image handling is crucial for many applications. This article provides a comprehensive guide to building a serverless image resizing solution using Python, Boto3, and AWS Lambda triggered by S3 events. This approach ensures cost-effectiveness, scalability, and automated image optimization for diverse business needs. Understanding and implementing such a solution not only boosts application performance but also exemplifies best practices in cloud resource management and FinOps.

Key Points

Building a serverless image resizing solution using Python, Boto3, and AWS Lambda.

Leveraging AWS S3 triggers to automate image processing upon upload.

Implementing image resizing logic within a Lambda function for scalability and cost efficiency.

Understanding business use cases that benefit from automated image resizing.

Utilizing infrastructure-as-code (IaC) for deploying Lambda functions with AWS SAM.

Optimizing cloud storage costs through effective resource management and FinOps principles.

Ensuring proper error handling and logging within the Lambda function for debugging.

Configuring environment variables for customizable image resizing parameters.

Deleting original images after resizing to minimize storage footprint.

Understanding the Serverless Image Resizing Solution

What is Serverless Image Resizing?

Serverless image resizing involves automatically adjusting the Dimensions and file size of images without managing underlying servers. This is achieved using cloud functions like AWS Lambda that execute code in response to triggers, such as uploading an image to an S3 bucket.

The system architecture usually includes:

  • S3 Buckets: One bucket for storing the original images (source bucket) and another for storing the resized images (target bucket).
  • AWS Lambda: A serverless compute service that runs the image resizing code.
  • S3 Triggers: Configured events that activate the Lambda function when an image is uploaded to the source bucket.
  • Python and Boto3: Python is the programming language used for the Lambda function, and Boto3 is the AWS SDK for Python, used to interact with AWS services, including S3.

This approach is highly scalable, cost-effective (you only pay for execution time), and reduces operational overhead compared to traditional server-based image processing solutions. By automating this process, applications can efficiently handle user-uploaded images, optimize website load times, and provide a better user experience.

Why Use a Serverless Approach?

Choosing a serverless architecture for image resizing offers several compelling advantages:

  • Cost Optimization: Pay-per-use model means you only incur costs when the Lambda function is actively resizing images.

    This is far more economical than running dedicated servers.

  • Scalability: Lambda automatically scales to handle varying workloads, ensuring your image processing can accommodate sudden traffic spikes without manual intervention.
  • Reduced Operational Overhead: With no servers to manage, you eliminate the burden of patching, updating, and maintaining infrastructure, allowing you to focus on code and application features.
  • Event-Driven Architecture: The system is triggered by S3 events, making it responsive and real-time, ensuring immediate resizing upon image upload.
  • Simplified Deployment: Using tools like AWS SAM (Serverless Application Model), you can define and deploy your entire infrastructure as code, streamlining the deployment process.

Furthermore, adopting a serverless approach aligns with FinOps principles, encouraging a better understanding of cloud resource usage and cost control.

Detailed Breakdown of the Image Resizing Process

Step-by-Step Workflow

Let's dive into the mechanics of this serverless setup:

  1. Image Upload: A user uploads an image to the designated S3 source bucket.

  2. S3 Event Trigger: This upload triggers an S3 event (specifically, ObjectCreated:*), which is configured to invoke the AWS Lambda function.

  3. Lambda Function Execution: The Lambda function is executed. It retrieves the uploaded image from the source bucket using the object key provided in the event data.

  4. Image Resizing: The Lambda function uses the Python Imaging Library (PIL, often accessed via the Pillow library) along with Boto3 to manipulate and resize the image based on predefined dimensions or a percentage reduction.

  5. Storing Resized Image: The resized image is then uploaded to the designated S3 target bucket.

  6. Optional Deletion: (As per the architecture in the video), the original image in the source bucket is then deleted.

    This step helps to manage storage costs and keep the source bucket tidy; however, it is a design choice depending on your need to keep original files.

  7. Logging: CloudWatch logs all operations performed by the Lambda function, aiding in monitoring, troubleshooting, and auditing.

This workflow ensures that every image uploaded is automatically processed and stored in the desired format and size, ready for use in your application.

AWS SAM: Streamlining Deployment

AWS SAM is invaluable in simplifying the deployment of serverless applications. It provides an easy-to-use command-line interface (CLI) and a template specification to define your serverless resources, including Lambda functions, S3 buckets, and event triggers.

Benefits of using AWS SAM include:

  • Infrastructure as Code (IaC): Define your entire application infrastructure in a single SAM template, promoting repeatability and version control.
  • Local testing: AWS SAM allows you to test your Lambda functions locally, mimicking the AWS environment, before deploying to the cloud.
  • Simplified Deployment: The sam deploy command automates the deployment process, creating the necessary AWS resources based on your SAM template.

The following table outlines the key components typically defined within a SAM template for this image resizing solution:

Resource Description
Lambda Function The core function that contains the image resizing logic, written in Python.
S3 Source Bucket The bucket where original images are uploaded.
S3 Target Bucket The bucket where resized images are stored.
S3 Event Trigger The configuration that triggers the Lambda function upon image upload to the source bucket.
IAM Role Defines the permissions for the Lambda function, allowing it to access S3 buckets and write to CloudWatch logs.
Environment Variables Configuration settings such as target bucket name, image size.

AWS SAM significantly reduces the complexity of deploying serverless applications, fostering a more efficient development and operations workflow.

Essential Code Snippets & Considerations

Let's examine the key code elements within the Lambda function written in Python. First the Python Imaging Library (PIL):

from PIL import Image
import io
import boto3

def resize_image(image_data, size):
    img_byte_arr = io.BytesIO(image_data)
    image = Image.open(img_byte_arr)
    width, height = image.size
    resize_ratio = size / float(width)
    new_height = int(resize_ratio * height)
    image = image.resize((size, new_height), Image.LANCZOS)

    resized_image = io.BytesIO()
    image.save(resized_image, image.format)

    return resized_image.getvalue()

This function takes the image data and a desired width (size) as input, calculates the appropriate Height to maintain aspect ratio, and resizes the image using a high-quality resampling filter (LANCZOS). The resized image is then saved into a byte stream.

Second, let's look into Boto3 (AWS SDK for Python):

s3 = boto3.client('s3')

def lambda_handler(event, context):
    source_bucket = event['Records'][0]['s3']['bucket']['name']
    object_key = event['Records'][0]['s3']['object']['key']
    size = int(os.environ['SIZE'])
    target_bucket = os.environ['TARGET_BUCKET_NAME']

    image_object = s3.get_object(Bucket=source_bucket, Key=object_key)
    image_data = image_object['Body'].read()

    resized_image = resize_image(image_data, size)

    target_object_key = f'{object_key.split('.')[0]}_{size}.png'

    s3.put_object(Bucket=target_bucket, Key=target_object_key, Body=resized_image)
    s3.delete_object(Bucket=source_bucket, Key=object_key)

    return {
        'statusCode': 200,
        'body': 'Image resized and uploaded successfully!'
    }

It gets the bucket name from S3, then gets the specific name of the object. Next gets the image resize size from the environment variable.

Key considerations for building and running are:

  • Error Handling: Implement robust error handling to catch exceptions during image retrieval, resizing, and uploading. This can include logging error messages and retrying failed operations.
  • Logging: Use Python’s logging module to Record important events and debug information, aiding in monitoring and troubleshooting.
  • Security: Ensure your Lambda function has the minimum necessary permissions to access S3 and CloudWatch. Avoid hardcoding credentials; instead, use IAM roles.
  • Configuration: Use environment variables to configure the target bucket name, image size, and other parameters, enabling flexibility and easier maintenance.

Step-by-Step Guide to Implementing the Solution

Step 1: Set Up AWS Credentials and Environment

First up, ensure that you have the AWS CLI installed and configured with appropriate credentials. This allows you to interact with AWS services from your local machine. Also ensure you have Python and pip installed, which is needed to use Boto3. Once you have the AWS CLI, Python and pip installed, configure your environment variables for easy use and deployment.

Step 2: Create S3 Buckets

Create two S3 buckets: one for the original images (source bucket) and another for the resized images (target bucket). Make sure to choose unique names for your buckets within the AWS global namespace. Remember that your buckets can be easily and quickly created, but it's important to consider costs and cleanup the buckets when you're done.

Step 3: AWS SAM Template Creation

Create a SAM template (sam.yml) to define your serverless application. This template will include resource definitions such as the Lambda function, S3 buckets, and event triggers.

Consider a basic SAM Template.

Resources:
 ImageResizerFunction:
 Type: AWS::Serverless::Function
 Properties:
 Handler: image_resizer.lambda_handler
 Runtime: python3.8
 CodeUri: .
 MemorySize: 128
 Timeout: 15
 Environment:
 Variables:
 TARGET_BUCKET_NAME: !Ref TargetBucket
 Events:
 Upload:
 Type: S3
 Properties:
 Bucket: !Ref SourceBucket
 Events: s3:ObjectCreated:*

TargetBucket:
 Type: AWS::S3::Bucket

SourceBucket:
 Type: AWS::S3::Bucket

Adjust to meet your sizing and budget needs. Note the location of the files and the correct version is referenced.

Step 4: Implement the Lambda Function

Develop the Lambda function using Python and Boto3. The function should:

  • Retrieve the uploaded image from the source bucket.
  • Resize the image using the Python Imaging Library (Pillow).
  • Upload the resized image to the target bucket.
  • Optionally delete the original image from the source bucket.
  • Consider adding error handling and try/except block.

Step 5: Build and Deploy the Application

Use the AWS SAM CLI to build and deploy your serverless application. This command packages your code and dependencies and deploys the infrastructure defined in your SAM template.

sam build
sam deploy --guided

Step 6: Test the Solution

Upload an image to the source bucket, which in turn will trigger the Lambda function. Next Verify the resized image appears in the target bucket. Check CloudWatch logs for any errors or performance bottlenecks.

Understanding AWS Lambda and S3 Pricing

AWS Lambda Pricing

Lambda pricing is based on the number of requests and the duration of execution. It's crucial to optimize your Lambda function's code and memory allocation to minimize execution time and, thus, costs. Consider factors like image size, resizing complexity, and the efficiency of your Python code.

Review AWS documentation for the most up to date information.

AWS S3 Pricing

S3 pricing is based on storage, data transfer, and requests. Storage costs depend on the storage class you choose (e.g., Standard, Intelligent-Tiering, Glacier). Regularly monitor and manage your S3 bucket contents to avoid unnecessary storage costs. S3 is the go to solution in this case, but you can consider more long term storage solutions such as Glacier.

Advantages and Disadvantages

👍 Pros

Cost-effective due to pay-per-use model.

Automatically scales to handle varying workloads.

Reduces operational overhead by eliminating server management.

Provides real-time image resizing through event-driven architecture.

👎 Cons

Requires familiarity with AWS services like Lambda, S3, and SAM.

Can introduce cold starts, affecting initial response times.

Requires careful monitoring and optimization to control costs.

Increased complexity relative to on-premise solution

Key Features of the Solution

Automated Image Resizing

Automatically resizes images upon upload, reducing manual effort and ensuring consistent image optimization.

Serverless Scalability

The system scales automatically based on demand, accommodating varying workloads without manual intervention.

Cost Efficiency

The pay-per-use pricing model of Lambda and S3 ensures you only pay for the resources you Consume.

Event-Driven Architecture

The system is triggered by S3 events, making it responsive and real-time.

Centralized Logging

CloudWatch logs provide detailed insights into the operations of the Lambda function, facilitating monitoring and troubleshooting.

Practical Use Cases for Serverless Image Resizing

E-commerce Platforms

Automatically generate thumbnails and optimized images for product listings, enhancing user experience and reducing page load times.

Social Media Applications

Process user-uploaded profile pictures and media content, ensuring compatibility across various devices and resolutions.

Content Management Systems (CMS)

Optimize images for web pages and articles, improving website performance and SEO rankings.

Online Photo Albums and Galleries

Automatically resize and optimize images for display, improving loading times and reducing bandwidth usage.

Bank storing check images

Resize images to improve inventory and ordering.

Auto dealers storing auto images

Resize images to improve inventory and ordering.

Frequently Asked Questions

What are the prerequisites for implementing this solution?
You need an AWS account, basic knowledge of Python and Boto3, familiarity with the AWS CLI and AWS Lambda, and an understanding of serverless architecture concepts. Having some working knowledge of the AWS SAM helps as well
How do I handle errors in the Lambda function?
Implement robust error handling using try...except blocks to catch exceptions during image retrieval, resizing, and uploading. Log error messages to CloudWatch and consider retrying failed operations.
Can I configure the image resizing parameters?
Yes, use environment variables to configure parameters such as the target bucket name and image size. This allows for easier maintenance and deployment across different environments.
How can I monitor the performance of my Lambda function?
Use CloudWatch metrics to monitor execution time, memory usage, and error rates. Consider implementing custom metrics for more granular insights into your function's behavior.

Related Questions

How do I secure my Lambda function?
Use IAM roles with the least privilege principle to grant your Lambda function only the necessary permissions to access S3 and CloudWatch. Avoid hardcoding credentials; instead, rely on IAM roles and environment variables.
What are the best practices for optimizing cloud resources?
Regularly review and optimize your cloud resources, including Lambda memory allocation, S3 storage classes, and data transfer costs. Consider implementing automated monitoring and cost control tools to identify and address potential inefficiencies.
What if the code returns an error in the lambda function?
Make sure your AWS SAM is configured properly by going over your code in the AWS SAM template. This will include checking the path for the file, if you are not referencing code in your sam template you will have an error returned.

Most people like