What is AWS Lambda with Image Processing Example

Anshul
4 min readMay 4, 2023

--

AWS Lambda is a serverless computing service that allows you to run code without provisioning or managing servers. Instead, you upload your code to AWS Lambda and it automatically runs and scales your application based on the incoming request. In this article, we will explore a real use case and example of AWS Lambda.

Use Case: Image Processing

Let’s say you have a website that allows users to upload images, and you want to process these images to create thumbnails. To do this, you need a serverless solution that can automatically resize images as they are uploaded.

What is AWS Lambda with Image Processing Example

Example: Building an Image Resizer with AWS Lambda

To build our image resizer, we will use the following AWS services:

  • Amazon S3: A scalable object storage service that allows you to store and retrieve data.
  • AWS Lambda: A serverless computing service that allows you to run code without provisioning or managing servers.
  • Amazon API Gateway: A fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale.

Transform your software development process and accelerate business growth with our comprehensive DevOps training.
DevOps MasterClass : Terraform Jenkins Kubernetes Ansible

Step 1: Create an S3 Bucket
The first step is to create an S3 bucket where users can upload their images. To do this, go to the S3 console and click on “Create Bucket”. Follow the prompts to create your bucket.

Step 2: Create a Lambda Function
Next, we need to create a Lambda function that will process the images. To do this, go to the Lambda console and click on “Create Function”. Choose “Author from scratch” and give your function a name. Select “Python 3.8” as the runtime, and choose the “Create a new role with basic Lambda permissions” option for the execution role.

In the function code, copy and paste the following code:

import boto3
import os
import sys
import uuid
from PIL import Image
import PIL.Image

s3_client = boto3.client('s3')

def resize_image(image_path, resized_path):
with Image.open(image_path) as image:
image.thumbnail(tuple(x / 2 for x in image.size))
image.save(resized_path)

def lambda_handler(event, context):
for record in event['Records']:
bucket = record['s3']['bucket']['name']
key = record['s3']['object']['key']
download_path = '/tmp/{}{}'.format(uuid.uuid4(), key)
upload_path = '/tmp/resized-{}'.format(key)
s3_client.download_file(bucket, key, download_path)
resize_image(download_path, upload_path)
s3_client.upload_file(upload_path, '{}-resized'.format(bucket), key)

This code uses the Python Imaging Library (PIL) to resize images to half their original size. It downloads the image from S3, resizes it, and then uploads the resized image back to S3.

Step 3: Configure the Lambda Function
Now that we have created our Lambda function, we need to configure it to run automatically when an image is uploaded to our S3 bucket. To do this, go to the S3 console and select your bucket. Click on the “Properties” tab, and then click on “Events”. Click on “Add notification”, and configure the following:

  • Event type: “All objects create events”
  • Prefix: leave blank
  • Suffix: “.jpg”
  • Send to: “Lambda function”
  • Lambda function: select the function you just created

Click on “Save” to save your configuration.

Step 4: Create an API Gateway
Now that we have configured our Lambda function to run automatically when an image is uploaded, we need to create an API Gateway to allow users to upload their images. To do this, go to the API Gateway console and click on “Create API”. Choose “REST API” and click on “Build”.

Now we need to create a resource that will contain our API methods. Click on “Create Resource” and give your resource a name.

Next, we need to create a method that will handle HTTP requests. Click on the resource you just created and click on “Create Method”. Choose the appropriate method for your API.

Step 5: Configure the Method
After creating the method, we need to configure it to work with our Lambda function. Click on the method and select “Lambda Function” as the integration type. Choose your Lambda function from the dropdown and click on “Save”.

Step 6: Deploy the API
After configuring the method, we need to deploy our API to make it publicly accessible. Click on the “Actions” dropdown and select “Deploy API”. Choose the appropriate deployment stage, such as “Production”.

Once the deployment is complete, you will see a URL that you can use to access your API. This URL can be shared with users who want to access your API.

Conclusion

Configuring a Lambda function with an API Gateway is a powerful way to build serverless APIs that can scale automatically based on user demand. By following the steps outlined above, you can easily build and deploy your own serverless APIs in AWS Lambda.

--

--

Anshul
Anshul

Written by Anshul

DevRel 🥑 DevOps / Cloud Engineer | Terraform, Ansible, Docker & Kubernetes Enthusiast 🐳 GCP | Jenkins | Terraform Certified

No responses yet