AWS Lambda function to create thumbnails of image on upload

Zeeshan Tariq
4 min readFeb 22, 2023

--

In today’s world, images play a crucial role in online content, and often, it is required to display them in different sizes and formats. Creating thumbnails of images is one such requirement, especially when it comes to improving the loading speed of a website or application. AWS Lambda, a serverless computing service, provides a simple and cost-effective solution to create thumbnails of images on upload. In this article, we will discuss how to use AWS Lambda function to create thumbnails of images on upload.

AWS Lambda is an event-driven computing service that allows developers to run code without provisioning or managing servers. It is a fully managed service that scales automatically based on the incoming requests. Lambda supports several programming languages, including Node.js, Python, Java, and C#. In our case, we will be using Node.js to write our Lambda function.

Before we start, we need to ensure that we have an AWS account and have set up the AWS Command Line Interface (CLI) on our local machine. We will also need to create an S3 bucket to store the original and thumbnail images.

Let’s start by creating a new Lambda function in the AWS Console. To create a new Lambda function, follow these steps:

  1. Log in to the AWS Console and navigate to the Lambda service.
  2. Click on the “Create function” button.
  3. Choose “Author from scratch” and give your function a name.
  4. Choose “Node.js 14.x” as the runtime.
  5. Choose “Create a new role with basic Lambda permissions” as the execution role.
  6. Click on the “Create function” button.

Now that we have created our Lambda function, we can start writing the code to create thumbnails of images on upload. Here’s the code that we will be using:

const AWS = require('aws-sdk');
const sharp = require('sharp');

const s3 = new AWS.S3();

exports.handler = async (event, context, callback) => {
const srcBucket = event.Records[0].s3.bucket.name;
const srcKey = event.Records[0].s3.object.key;

const dstBucket = `${srcBucket}-thumbnails`;
const dstKey = `thumbnails/${srcKey}`;

const image = await s3.getObject({ Bucket: srcBucket, Key: srcKey }).promise();
const resizedImage = await sharp(image.Body).resize(200).toBuffer();

await s3.putObject({ Bucket: dstBucket, Key: dstKey, Body: resizedImage }).promise();

callback(null, `Successfully resized ${srcBucket}/${srcKey} and uploaded to ${dstBucket}/${dstKey}`);
};

Let’s go through the code line by line:

  1. We import the necessary packages, including the AWS SDK and Sharp, a Node.js image processing library.
  2. We create a new instance of the S3 class from the AWS SDK.
  3. We define the Lambda function handler, which will be called whenever a new object is uploaded to the S3 bucket.
  4. We extract the source bucket and key from the S3 event object.
  5. We define the destination bucket and key for the thumbnail image.
  6. We use the S3 getObject method to get the original image from the source bucket and key.
  7. We use the Sharp library to resize the image to a width of 200 pixels.
  8. We use the S3 putObject method to upload the resized image to the destination bucket and key.
  9. We call the Lambda function callback with a success message.

Now that we have written our Lambda function, we need to configure the S3 bucket to trigger the Lambda function whenever a new object is uploaded. To do this, follow these steps:

  1. Navigate to the S3 service in the AWS console.
  2. Select the S3 bucket where you want to create thumbnails.
  3. Click on the “Properties” tab.
  4. Click on the “Events” section, then click on the “Add notification” button.
  5. Enter a name for the notification, such as “CreateThumbnail”.
  6. Select “All object create events” as the event type.
  7. In the “Send to” section, choose “Lambda function” as the destination.
  8. Select the Lambda function you created from the drop-down list.
  9. Click on the “Save” button to create the notification.

That’s it! Now, whenever a new object is uploaded to the S3 bucket, the Lambda function will automatically create a thumbnail image and upload it to the destination bucket. You can test this by uploading an image to the source bucket and checking the destination bucket for the thumbnail image.

In conclusion, using AWS Lambda to create thumbnails of images on upload is a simple and cost-effective solution that can significantly improve the loading speed of websites and applications. By following the steps outlined in this article, you can easily create a Lambda function that will automatically create thumbnail images whenever new objects are uploaded to an S3 bucket.

--

--

Zeeshan Tariq
Zeeshan Tariq

Written by Zeeshan Tariq

Software Architect @ SoftPyramid

No responses yet