X
Popular Searches

How to Automatically Compress Images in S3 with Lambda

s3 logo

One of the largest costs of AWS is bandwidth—it’s expensive, especially if your app is dependant on serving content. Compressing the images you serve can reduce your bill significantly, as well as save you money on the storage costs.

Why Lower the Size?

The simplest way to lower bandwidth is to lower the size of the objects you’re serving with compression and encoding. It can also reduce your storage costs as well, which can save a lot of money over time. JPEG has quality built into the standard; JPEGs encoded at 70% or so are still sharp and nearly indistinguishable from 100% quality (at which no JPEG intended for web distribution should be encoded).

Also, if you’re enabing users to upload their own images and aren’t processing them in some way, you’re opening yourself up to abuse from users uploading multiple gigabytes of gigantic photos and slowing down your application.

AWS doesn’t have a built in tool for doing this, so you’ll have to use Lambda, AWS’s service for running code in response to triggers without provisioning servers. If you’d like to encode video as well, AWS has a service for this, as it’s a more CPU-intensive task.

Getting Started

Head over to the Lambda Management Console, and click “Create Function.”

create function

Luckily, there’s already a prebuilt app on the Lambda serverless app repository that can handle basic image compression and is perfect for this task. Select “Browse Serverless App Repository.”

browse app repo

Then, search for “compress,” by evanchiu:

search for compress

This application uses transforms, which owns an S3 bucket and watch it for uploaded files. When a file is uploaded, the function runs, compresses the image, and puts it in a destination bucket.

The input bucket is created by the function, but the output bucket will need to be created from the S3 Management Console:

s3 console

Then, back in the Lambda Console, enter in the name of your destination bucket in the Application Settings, then give a name for the source bucket that will be created. Make sure this doesn’t exist already, as the app must own the bucket.

You can specify the JPEG quality here. While 70% or so should be a good number, you can do your own testing to see what works best for your application.

In Applications Settings, enter the name of the destination bucket and the name of source bucket, along with the JPEG quality.

Click on “Deploy,” and the app should be up and running. Within a few minutes, you’ll see the newly created source bucket owned by the application. If you upload an image into it, you’ll quickly see the compressed image created in the destination bucket.

If you’d like to modify the application or add your own functionality, you can do so by clicking on the “Functions” tab to the right:

By clicking on the "Functions" tab, you can modify the application or add your own functionality.

Here, you can also modify the triggers that cause the function to run, and edit the settings you set when creating the function.

Anthony Heddings Anthony Heddings
Anthony Heddings is the resident cloud engineer for LifeSavvy Media, a technical writer, programmer, and an expert at Amazon's AWS platform. He's written hundreds of articles for How-To Geek and CloudSavvy IT that have been read millions of times. Read Full Bio »

The above article may contain affiliate links, which help support CloudSavvy IT.