AWS LAMBDA IMAGE RESIZE HOW TO
This blog will walk you through how to dynamically resize your image so that you don’t have to do it yourself. The problem is, the image is 6MB in size and you’re too lazy to resize it before uploading it to your website.
Now, this photograph is worth sharing on your website. Got it from )īoom! You just became a photographer. Good boys! (not my photos, unfortunately. You bring your phone which has an overcomplicated camera with you. Thanks for reading! A note from Plain Englishĭid you know that we have launched a YouTube channel? Every video we make will aim to teach you something new.It’s Saturday morning and you need to take your dog (or a cat. I’ve seen various approaches online for this using the serverless framework, but as we’re using Claudia.js and developing on Macs, I thought I’d write up what I did, just in case it saves somebody a bit of time.Īny thoughts or suggestions for improvements would be greatly appreciated in the comments.
AWS LAMBDA IMAGE RESIZE CODE
This will create your lambda referencing the Sharp layer and your image manipulation code should work like a dream. Notice there’s a layers parameter which takes the ARN from our layer (this can be an array of comma-separated ARNs if more than one layer is needed) Deploymentĭeploying your lambda function code with Claudia.js is pretty straightforward: claudia create \ -description API for OMW Photos Backend \ -config deploy/dev/photos-dev \ -region eu-west-2 \ -handler build/functions/photos.handler \ -layers arn:aws:lambda:eu-west-2:XXXXXXXXX:layer:sharp:1 \ -name photos-dev \ -runtime nodejs12.x \ -role arn:aws:iam::XXXXXXXXX:role/lambda-some-role If there’s a better way to achieve this, I’d love to hear about it in the comments. The location from which it’s retrieved is the node_modules/sharp folder in /opt - this the location where the AWS Lambda environment copies in referenced layers. What’s happening here is that the sharp dependency is loaded on-demand within the function that uses it. Since the function is going to load this from a layer rather than a locally installed dependency you need to lazy-load sharp to circumvent the dependency checks.įor example: export async function addPhoto(contents) Making use of your new Sharp layerĬlaudia.js performs some handy validation prior to deployment including ensuring that any dependencies are present. I use Claudia.js for Lambda and API Gateway deployments too which while it greatly simplifies the deployment process, does add an extra level of complexity when trying to get all these different parts to play nicely together.Īt the end of the ARN is :1 - this will increment by one with each new version that’s uploaded. It also requires you to use Docker to create the files to package up which seemed to be a long-winded, tool-heavy, approach to me. I’d tried following instructions here but they’re for an older version and I simply couldn’t get it to work doing it that way - mentioned files are no longer are in the sharp package. How to reference sharp from within the lambda.
AWS LAMBDA IMAGE RESIZE MAC
How to create a layer with the correct binaries from a Mac environment.Sharp is pretty big so including it within the lambda function package is really a no-go and it should, therefore, be in its own layer.This means developing on a Mac and deploying to Lambda (a Linux container) means the binaries used for Sharp are incompatible