How to resize images in S3 bucket with using GOLang as a lambda function

Ümit Kaş
4 min readJan 19, 2021

Data consumption is one of the forgotten things in mobile development, mostly large images are spearheading. Caching the images is reducing the data consumption but not a solution. In this article, you can find an AWS Lambda resizer solution for your large-sized images stored in S3 Bucket using GoLang.

How does it Work?

The aim is to trigger an AWS Lambda function when an image is uploaded to an S3 Bucket. The lambda function is coded with golang, and the imaging is the resizer library. Codes are available on Github.

Let's begin with creating GoLang project from starch;

Let’s create a go project and add imaging and aws-sdk-go dependencies.

Start Coding with Reading Configurations from Environment

Lets start with our configuration.go file. This file reads data from environment variables and creates a Configuration struct that contains the image and aws-related parameters.

Important note, lambda has only permission to write to directory /tmp therefore LocalImageDirectory is set as constant.

Downloading the Image

When lambda function is triggered, the uploaded file should be downloaded from s3 storage to the local project directory which is /temp to resizing it. The following function downloads image to /tmp directory and returns the image path.

Let’s Resize the Downloaded Image

Now time to resize the image, The following functions resize the source image by given properties on the environment variable and returns the name of files.

Uploading Resized Images to S3 Bucket

Time to upload images back to s3 bucked, the following functions upload the resized images in /tmp directory.

Time to Handle Requests

AWS only allows you to define one handler per Lambda function that receives the request and response. this function reads the configuration from the environment variables, downloads, resizes, uploads, and lastly deletes the created images.

Let’s build and zip the executable file app.

Now Time To Deploy Lambda Function

First thing first we need to create a lambda function.

The following action will be adding an S3 Put trigger to our lambda function

Suggestion, lambda functions can be triggered recursively, when you upload the resized images to s3 bucket back, therefore, original files should have a prefix, such as a directory.

After adding a trigger, lambda will be shown as follows.

Let’s upload our executable file in a zip from Function Code’s action menu.

Runtime Settings another important part of our configuration. Handler name must be our executable file name which is app

The last Configuration is the setting environment variables.

Time to Test Lambda Function

Create an event template as s3-put and configure object key as an exist image in your bucket

And Test It

Conclusion

In this article, we create an image resizer application with golang as a aws lambda function, where images are stored in s3 bucket. Project is available on Github.

References

--

--