Skip to main content

Setting up Lambda for image resizing

Compiling sharp for x86_64 architecture

To use Sharp for Node.js properly, we need to compile it on aan x86_64 Linux server. To do so:

- Create a Node.js AWS Lightsail instance ($3.5 tier will do the job)
-, Unzipthen imageresizer-main.zip, removerun the node_modulesfollowing:

folder,
mkdir sharpfolder
cd sharpfolder
npm i sharp
zip it-r sharp.zip node_modules/

You can now download this zip via WinSCP, and upload toit Lightsailas instance
- Run npm install inside folder directory
- Rezip all folder contents and upload to AWSa Lambda layer.

Lambda function configuration

Environment Variables

- When setting the environment variables configuration for the Lambda function, make sure CDN_URL includes the https:// at the front to avoid the code returning an incorrect header URLURL.

Execution Role

CreateIf you don't already have an IAM user,user e.g.for resize-lambda-user,invoking withthe S3 PutObjectfunction, and GetObjectan permissionsIAM (S3role Objectto Lambda is not needed). Use this user's access key and secret keyserve as environmentthe variablesexecution role, you will need to create both of these for the Lambdafunction function.to work properly. See this article for how to do that: Implementing an image resizing solution yourself.