Score:0

AWS Lambda error on build Node with Sharp Library

jp flag

I have a problem to build my lambda handler. I'm trying to use sharp library to do an image resize on S3.

My code:

S3.getObject({
    Bucket: bucketName,
    Key: objectKey
}, (err, data) => {
    SHARP(data.Body).resize(250, 250).toFormat("jpg").toBuffer().then((buffer) => {
        S3.putObject({
            Body: buffer,
            Bucket: bucketName,
            Key: objectKey,
            ContentType: "image/jpeg",
            ContentDisposition: "inline",
        })
    })
});

My node dependencies:

  "dependencies": {
    "aws-sdk": "^2.637.0",
    "dotenv": "^10.0.0",
    "mysql": "^2.18.1",
    "sharp": "^0.29.1",
    "util": "^0.12.4"
  },
  "devDependencies": {
    "esbuild": "^0.12.27",
    "nodemon": "^2.0.12"
  }

My code to build:

esbuild --bundle --minify --platform=node --target=node12 --outdir=build index.js && cd build && zip index.js.zip index.js

AWS Lambda error:

Cannot find module '../build/Release/sharp-linux-x64.node'
Require stack:
- /var/task/index.js
- /var/runtime/UserFunction.js
- /var/runtime/index.js

Possible solutions:
- Install with the --verbose flag and look for errors: "npm install --ignore-scripts=false --verbose sharp"
- Install for the current runtime: "npm install --platform=linux --arch=x64 sharp"
- Consult the installation documentation: https://sharp.pixelplumbing.com/install
Tim avatar
gp flag
Tim
This question might be better on server fault. The message says it can't find sharp, but exactly why is more a question for developers than infrastructure people I think. I use AWS SAM to do deployments of the odd bit of software I need to deploy
Matheus Pereira avatar
jp flag
I thought about AWS SAM, but how I'm new on this world, I have fear to configure that in a development machine, but is a good way, I can try this. Thanks for the tips, @Tim!
Tim avatar
gp flag
Tim
AWS SAM may help with dependencies and deployment. I found it easier than trying to do packaging and such myself. It's not super complex but it does take a little bit of time to learn and get working, but should hopefully save time overall.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.