2

I am new to using AWS Amplify and AWS Lambda and I'm facing a problem that I can't solve on my own.

Explanation:

I ran the command:

amplify add storage

This command generated for me the trigger below:

exports.handler = async function (event) {
  console.log('Received S3 event:', JSON.stringify(event, null, 2));
  const bucket = event.Records[0].s3.bucket.name;
  const key = event.Records[0].s3.object.key;
  console.log(`Bucket: ${bucket}`, `Key: ${key}`);
};

What I want to do?

I would like to resize the images that are uploaded to my bucket.

What's the problem?

My problem is that I can't install sharp package.

Because of yarn, I cannot run this command:

npm install sharp

With npm, I get this error (remove package-lock.json file don't resolve the problem):

warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.

So I ran this command:

yarn add sharp

But I still have errors. I created a video that I uploaded to Youtube:
https://youtu.be/iZANbmVhhp4

Also I tested that but it's not works:

↓ ↓ ↓

https://sahanamarsha.medium.com/resizing-s3-images-with-aws-lambda-trigger-ca4cf2372d0e

https://github.com/SahanAmarsha/image-resizer-lambda/blob/master/package.json

↑ ↑ ↑

Creating thumbnails is very common and I don't understand why I can't do it.
Can you explain to me step-by-step how to do it?
So that I can understand why I can't.

Regards

Additional infos

Here is the Execution result:failed Details I get in the AWS console:

{
  "errorType": "Error",
  "errorMessage": "\nSomething went wrong installing the \"sharp\" module\n\nCannot find module '../build/Release/sharp-linux-x64.node'\nRequire stack:\n- /var/task/node_modules/sharp/lib/sharp.js\n- /var/task/node_modules/sharp/lib/constructor.js\n- /var/task/node_modules/sharp/lib/index.js\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/Runtime.js\n- /var/runtime/index.js\n\nPossible solutions:\n- Install with verbose logging and look for errors: \"npm install --ignore-scripts=false --foreground-scripts --verbose sharp\"\n- Install for the current linux-x64 runtime: \"npm install --platform=linux --arch=x64 sharp\"\n- Consult the installation documentation: https://sharp.pixelplumbing.com/install",
  "trace": [
    "Error: ",
    "Something went wrong installing the \"sharp\" module",
    "",
    "Cannot find module '../build/Release/sharp-linux-x64.node'",
    "Require stack:",
    "- /var/task/node_modules/sharp/lib/sharp.js",
    "- /var/task/node_modules/sharp/lib/constructor.js",
    "- /var/task/node_modules/sharp/lib/index.js",
    "- /var/task/index.js",
    "- /var/runtime/UserFunction.js",
    "- /var/runtime/Runtime.js",
    "- /var/runtime/index.js",
    "",
    "Possible solutions:",
    "- Install with verbose logging and look for errors: \"npm install --ignore-scripts=false --foreground-scripts --verbose sharp\"",
    "- Install for the current linux-x64 runtime: \"npm install --platform=linux --arch=x64 sharp\"",
    "- Consult the installation documentation: https://sharp.pixelplumbing.com/install",
    "    at Object.<anonymous> (/var/task/node_modules/sharp/lib/sharp.js:34:9)",
    "    at Module._compile (internal/modules/cjs/loader.js:1085:14)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)",
    "    at Module.load (internal/modules/cjs/loader.js:950:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:790:12)",
    "    at Module.require (internal/modules/cjs/loader.js:974:19)",
    "    at require (internal/modules/cjs/helpers.js:101:18)",
    "    at Object.<anonymous> (/var/task/node_modules/sharp/lib/constructor.js:8:1)",
    "    at Module._compile (internal/modules/cjs/loader.js:1085:14)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)"
  ]
}

Here is the Log output I get in the AWS console:

the installation documentation: https://sharp.pixelplumbing.com/install","stack":["Error: ","Something went wrong installing the \"sharp\" module","","Cannot find module '../build/Release/sharp-linux-x64.node'","Require stack:","- /var/task/node_modules/sharp/lib/sharp.js","- /var/task/node_modules/sharp/lib/constructor.js","- /var/task/node_modules/sharp/lib/index.js","- /var/task/index.js","- /var/runtime/UserFunction.js","- /var/runtime/Runtime.js","- /var/runtime/index.js","","Possible solutions:","- Install with verbose logging and look for errors: \"npm install --ignore-scripts=false --foreground-scripts --verbose sharp\"","- Install for the current linux-x64 runtime: \"npm install --platform=linux --arch=x64 sharp\"","- Consult the installation documentation: https://sharp.pixelplumbing.com/install","    at Object.<anonymous> (/var/task/node_modules/sharp/lib/sharp.js:34:9)","    at Module._compile (internal/modules/cjs/loader.js:1085:14)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)","    at Module.load (internal/modules/cjs/loader.js:950:32)","    at Function.Module._load (internal/modules/cjs/loader.js:790:12)","    at Module.require (internal/modules/cjs/loader.js:974:19)","    at require (internal/modules/cjs/helpers.js:101:18)","    at Object.<anonymous> (/var/task/node_modules/sharp/lib/constructor.js:8:1)","    at Module._compile (internal/modules/cjs/loader.js:1085:14)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)"]}
2022-10-26T03:30:59.951Z    undefined   ERROR   Uncaught Exception  {"errorType":"Error","errorMessage":"\nSomething went wrong installing the \"sharp\" module\n\nCannot find module '../build/Release/sharp-linux-x64.node'\nRequire stack:\n- /var/task/node_modules/sharp/lib/sharp.js\n- /var/task/node_modules/sharp/lib/constructor.js\n- /var/task/node_modules/sharp/lib/index.js\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/Runtime.js\n- /var/runtime/index.js\n\nPossible solutions:\n- Install with verbose logging and look for errors: \"npm install --ignore-scripts=false --foreground-scripts --verbose sharp\"\n- Install for the current linux-x64 runtime: \"npm install --platform=linux --arch=x64 sharp\"\n- Consult the installation documentation: https://sharp.pixelplumbing.com/install","stack":["Error: ","Something went wrong installing the \"sharp\" module","","Cannot find module '../build/Release/sharp-linux-x64.node'","Require stack:","- /var/task/node_modules/sharp/lib/sharp.js","- /var/task/node_modules/sharp/lib/constructor.js","- /var/task/node_modules/sharp/lib/index.js","- /var/task/index.js","- /var/runtime/UserFunction.js","- /var/runtime/Runtime.js","- /var/runtime/index.js","","Possible solutions:","- Install with verbose logging and look for errors: \"npm install --ignore-scripts=false --foreground-scripts --verbose sharp\"","- Install for the current linux-x64 runtime: \"npm install --platform=linux --arch=x64 sharp\"","- Consult the installation documentation: https://sharp.pixelplumbing.com/install","    at Object.<anonymous> (/var/task/node_modules/sharp/lib/sharp.js:34:9)","    at Module._compile (internal/modules/cjs/loader.js:1085:14)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)","    at Module.load (internal/modules/cjs/loader.js:950:32)","    at Function.Module._load (internal/modules/cjs/loader.js:790:12)","    at Module.require (internal/modules/cjs/loader.js:974:19)","    at require (internal/modules/cjs/helpers.js:101:18)","    at Object.<anonymous> (/var/task/node_modules/sharp/lib/constructor.js:8:1)","    at Module._compile (internal/modules/cjs/loader.js:1085:14)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)"]}
START RequestId: 0df51e26-e55f-42a0-8716-f2507ba7ff78 Version: $LATEST
END RequestId: 0df51e26-e55f-42a0-8716-f2507ba7ff78
REPORT RequestId: 0df51e26-e55f-42a0-8716-f2507ba7ff78  Duration: 2382.57 ms    Billed Duration: 2383 ms    Memory Size: 128 MB Max Memory Used: 15 MB  
Unknown application error occurred
Error

Here is my package.json:

{
  "name": "S3Triggeref35961b",
  "version": "2.0.0",
  "description": "Lambda function generated by Amplify",
  "main": "index.js",
  "license": "Apache-2.0",
  "dependencies": {
    "sharp": "^0.31.1"
  },
  "scripts": {
    "amplify:S3Triggeref35961b": "cd amplify/backend/function/S3Triggeref35961b/src && npm install && rm -rf node_modules/sharp && SHARP_IGNORE_GLOBAL_LIBVIPS=1 npm install --arch=x64 --platform=linux --libc=glibc sharp && cd -"
  }
}

Here is my index.js:

const sharp = require("sharp");

exports.handler = async function (event) {
  console.log("Hello World!");
};
jonathan
  • 105
  • 7

0 Answers0