Build a resize images tool with AWS (S3, Lambda, API Gateway) at Golang
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.
- 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.
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
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:
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.
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.
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.