Build a resize images tool with AWS (S3, Lambda, API Gateway) at Golang

Duc Ho
4 min readApr 21, 2019

--

Experience of the user is really important when they first come to your website, so the first sight and make them feel impressive that is the beautiful’s website that you made. Besides that, the speed of loading contents of the website is also important too. The factor usually makes the website slowly that is static files, and the image is one of them. For solving this problem, we should resize or zip the image after they upload into the storage server. After resize or zip we also make sure the quality of the image was not changed too much. I mean, we must let the quality around from 85–95 percent from the original image.

Architecture

In this tutorial, I would like to guide you on how to make a simple resize images tool with AWS S3, Lambda and API Gateway.

First, let’s have a look a bit with the architecture at above.

  1. The user will go to AWS APIGateway to send the request and receive the response from its.
  2. The AWS Lambda will be integrated with AWS API Gateway and AWS S3 for do its business logic.
  3. S3 will storage the original and the resized version of the image.
  4. The benefits of this architecture are:
  5. We will get huge benefits from Serverless architecture from AWS Lambda. We won’t worry about how to set up a server or manage it, AWS Lambda will help us.
  6. AWS Lambda also helps us scale up the service when needed.
  7. Real-time is a really nice feature to provide the best experience to the user.

5. AWS Lambda is supported Golang very good, and we would like to get benefits from Golang to do this tool.

6. It will help us decrease the cost of storage and bandwidth when the user serves static files.

Before we going to start, I won’t show step by step to do this tool, all code are simple and easy to understand, if you have confusing, please lets your comment at below.

Coding & Expected Implementation

Show me the code:

Firstly, we want to build a request likely:

https://your-api-gateway-endpoint/dimension_crop_filename.format

For example, I would like to resize my original image to 600x600 and crop center with my endpoint has deployed on AWS API Gateway:

https://kb2sf4lrd3.execute-api.ap-southeast-1.amazonaws.com/production/600x600_center_beeketing.jpg

The original image before be resized
The new images have resized by many different anchors

S3 Configuration

Firstly, go to AWS and create the S3 Bucket and config bucket policy as at below

You may want to change the resource name of S3 to make it work!

Lambda Configuration

Next, set up a Lambda function on AWS by these configs:

  • Timeout 20s
  • Environment variables: bucket, original_folder, resized_folder, regional
  • Add execution roles: AWS S3 Full Access, and AWS Basic Execution Role
  • You may want to reference with this SAM file if you familiar with it:
These are environment variables configuration that is required
Policies needed for AWS Lambda to execute its business

API Gateway Configuration

Next, set up an API Gateway:

  • Setup empty root path starts by /
  • Setup resource in root path by using /{proxy+}
  • Setup method GET in resource integrate with Lambda function that you have created before.
Setup routing path for your API Gateway
Integrate the HTTP method with AWS Lambda

Build & Upload Source Code

Pull the repository at above and run the terminal with these are commands for upload binary file to AWS Lambda.

GOOS=linux go build main.gozip resizeimage.zip ./main

Edit Handler by main (main.go) and upload file that you just created and click Save to deploy Lambda code.

Deploy API

Finally, go to API Gateway again, and click selects the GET Method and select Deploy API.

Deploy your API after upload new code and change configuration of the stage
Use invoke URL just have shown up to test your API

That all.

Thank you for watching, if you have any comment or question, please lets it at below.

If you don’t mind. Please buy me a coffee for supporting me in the next posts.

--

--

Duc Ho
Duc Ho

Written by Duc Ho

Fall in love with technologies and coding.

No responses yet