Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

652
Views
File is missing when trying to resize in Sharp in AWS Lambda function

I am sincerely puzzled by this. I am simply trying to invoke a Lambda function from within a Node JS application and perform image resizing on it. The invocation succeeds and the function is called (I can pass parameters to it and see them returned), however when I attempt to use Sharp for image resizing, I get the strange error that the input file is missing. It's not, as I can definitively confirm that the buffer is indeed passed to the Lambda.

I can confirm that using Sharp locally works. In the invocation I am JSON stringifying the payload, and I can confirm that the parameters are indeed passed to and are received by the Lambda. I am mapping them correctly from the first 'event' argument in the invocation. However the inclusion of Sharp within the Lambda just simply breaks.

I have created a new 'Sharp' project and have installed the node modules. These are packaged with the Lambda's index function into an archive and this is uploaded into the zip. The one thing that is quite different is that in order to use Sharp inside of Lambda, I had to install the linux version.

npm install --arch=x64 --platform=linux sharp

I have a test script in the Lambda where I have copied over a complete payload including a proper buffer. Both the test script and my local machine throw the same error:

{
  "errorType": "Error",
  "errorMessage": "Input file is missing",
  "trace": [
    "Error: Input file is missing"
  ]
}

This is my Lambda function.

const sharp = require('sharp');

exports.handler = async (event) => {
  return await sharp(event.buffer)
    .resize({
      width: event.maxWidth,
      height: event.maxHeight,
      fit: 'inside',
      withoutEnlargement: true
    })
    .jpeg({
      quality: 80
    })
    .toBuffer();
}

And this is my invocation.

const thumbImage = await lambda.invoke({
   FunctionName: 'sharp-resize',
   Payload: JSON.stringify({ buffer: file.buffer, maxWidth, maxHeight })
}).promise();

Any ideas or assistance would be appreciated.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I finally figured this out, and it really was such a simple thing. When chaining the buffer, Lambda needed to recreate the buffer from the event.buffer property. When the function returned the resized image, I chained it again before recreating the buffer on receipt. Then it was a simple matter of posting the new buffer to S3.

My final lambda:

 const sharp = require('sharp'); exports.handler = async (event) => { let buffer = Buffer.from(event.buffer.data); buffer = await sharp(Buffer.from(buffer)) .resize({ width: event.maxWidth, height: event.maxHeight, fit: 'inside', withoutEnlargement: true }) .jpeg({ quality: 80 }) .toBuffer(); } return { statusCode: 200, headers: { "Access-Control-Allow-Origin": "*" }, buffer: JSON.stringify(buffer) }; }

And my invocation and post to S3:

 const thumbImage = await lambda.invoke({ FunctionName: 'sharp-resize', Payload: JSON.stringify({ extension, buffer: file.buffer, maxWidth, maxHeight }) }).promise(); const payload = JSON.parse(thumbImage.Payload); const buffer = JSON.parse(payload.buffer); const parsedBuffer = Buffer.from(buffer.data); response.thumbImage = await uploadToS3({ key: amazonS3ThumbKey, file: parsedBuffer });
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!