![aws lambda image resize aws lambda image resize](https://i0.wp.com/matildamatinee.com/wp-content/uploads/2021/09/AWS_Lambda005.jpg)
Until next time, be curious and have fun."principalId": "AWS:AIDAINPONIXQXHT3IKHL2" If you liked it, slap that tiny unicorn so more people here on dev.to will see this article. Writing the article wasn’t that bad either! Hope you guys and girls enjoyed reading it as much as I enjoyed writing it.
#AWS LAMBDA IMAGE RESIZE CODE#
I had an absolute blast writing this open-source code snippet. If you want to read some of my previous serverless musings head over to my profile or join my serverless newsletter! If you follow along with the instructions there, you’ll be able to have this image resize on-the-fly microservice up and running in no time at all. We at Dashbird use clusters of containers for our core features that interact heavily with our databases while offloading all the rest to lambda functions, queues, streams and other serverless services on AWS.Īnd, of course, here’s the repo once again, give it a star if you want more people to see it on GitHub. To conclude this little showdev session, I’d like to point out using serverless as a helper to support your existing infrastructure is incredible. Starting with the Dockerfile, here's what you need to add. The hard part will be writing the bash script to run all commands and deploy our function and bucket. Here comes the fun part! We’ll create a Dockerfile and docker-compose.yml file to create our Amazon Linux container and load it with. Then resize the original image to the requested dimensions, and put it back to the bucket at the exact route it was requested from. If no image is found, the bucket will trigger the 404, which redirects to our lambda function. This lets us serve images from it and utilize the 404 error redirect rule. Once we enable the static website hosting option on our bucket, it will behave like any website. Type: AWS::S3::BucketPolicy # add policy for public read access create constants const BUCKET = Ĭonst URL = ` $/resize?key=" # route resize.js // require modules const stream = require( 'stream') First, define the helper functions we need in order to create the streams, then create the handler function itself which our lambda will invoke.
#AWS LAMBDA IMAGE RESIZE HOW TO#
Because images can get quite large and we don’t want to risk loading several megabytes into the lambda function’s memory, I’ll show you how to use Node.js streams to read the image from an S3 stream, pipe it to Sharp, and then write it back to S3, once again, as a stream. Profile: serverless-admin # or change to 'default'Īs you can see we’re grabbing a bunch of values from the env and also allowing the function to access the BUCKET we specified and some X-Ray telemetry. Service: image-resize-on-the-fly-functions The best way of explaining this complex structure will be with an image. With that all out of the way let’s build some stuff.
#AWS LAMBDA IMAGE RESIZE INSTALL#
Because AWS Lambda is running on Amazon Linux, we need to npm install the packages on an Amazon Linux instance before running sls deploy. This image-resize module has binaries that need to be built on the same operating system as it will run on. The funny thing here is that we need to use Docker to deploy this service because of Sharp. Note : Please install Docker and Docker Compose before continuing with this tutorial. Write the AWS Lambda function configuration.Here’s an overview of what we’ll be doing.
![aws lambda image resize aws lambda image resize](https://i.stack.imgur.com/qP4oT.png)
Pretty cool huh? Here’s a diagram, because who doesn’t love diagrams.īecause I already assume you know how to use the Serverless Framework and have already been introduced to the basics of serverless, Docker and AWS, I’ll immediately jump into the gist of things. Every subsequent request to the image of that size will be served from the bucket.
![aws lambda image resize aws lambda image resize](https://d2908q01vomqb2.cloudfront.net/77de68daecd823babbb58edb1c8e14d7106e83bb/2020/02/11/Toptal-AWS-Lambda-7.1.png)
This scenario will only resize an image for a given set of dimensions once. It’ll grab the existing image, resize it, return it back to the bucket and then serve it from the bucket. If you need a resized version, you’ll request the image and supply the height and width. The images will be stored in an S3 bucket and, once requested, will be served from it. Today we’ll build an AWS Lambda function to resize images on-the-fly. Luckily enough, there is a way to solve the issue of bad image optimization with little to no hassle. Check out the code here.īut if you want to follow along and learn how to do it yourself, keep reading. I’ve built and open-sourced a snippet of code that automates the process of creating and deploying an image resize function and an S3 bucket with one simple command.