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.
So, 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.
This tutorial has referenced from the tutorial of AWS at: https://aws.amazon.com/blogs/compute/resize-images-on-the-fly-with-amazon-s3-aws-lambda-and-amazon-api-gateway/
First, let's have a look a bit with the architecture at above.
- The user will go to AWS APIGateway to send the request and receive the response from its.
- The AWS Lambda will be integrated with AWS API Gateway and AWS S3 for do its business logic.
- S3 will storage the original and the resized version of the image.
- The benefits of this architecture are:
- 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.
- AWS Lambda also helps us scale up the service when needed.
- 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.
Firstly, we want to build a request likely:
For example, I would like to resize my original image to 600x600 and crop center with my endpoint has deployed on AWS API Gateway:
After resize successfully, we will be redirected to file's endpoint on AWS S3:
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!
Next, set up a Lambda function on AWS by these configs:
- Timeout 20s
- Environment variables:
- 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: https://gist.github.com/ducmeit1/938c52b0130e613d5c80cfc2f974d071#file-sam-resize-image-yaml
Next, set up an API Gateway:
- Setup empty root path start by
- Setup resource in root path by use
- Setup method GET in resource integrate with Lambda function that you have created before.
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.go
zip resizeimage.zip ./main
Edit Handler by main (main.go) and upload file that you just created and click Save to deploy Lambda code.
Finally, go to API Gateway again, and click selects the GET Method and select Deploy API.
Thank you for watching, if you have any comment or question, please lets it at below.
The original post at: https://ducmeit.me/build-a-resize-images-tool-with-aws-s3-lambda-api-gateway-at-golang/