3

I am pretty new to aws amplify and have an amplify app that has a backend lambda function which uses sharp as a dependency. When I push to git to trigger a deploy, my build is failing I think due to vips/vips8 a sharp dependency not being found.

Relevant log

2022-05-31T18:26:18.714Z [INFO]: [0mError: Packaging lambda function failed with the error [0m
                                 [0mCommand failed with exit code 1: npm install --no-bin-links --production[0m
                                 [0msh: prebuild-install: command not found[0m
                                 [0m../src/common.cc:24:10: fatal error: vips/vips8:  [0m
                                 [0m #include <vips/vips8>[0m
                                 [0m          ^~~~~~~~~~~~[0m
                                 [0mcompilation terminated.[0m
                                 [0mmake: *** [Release/obj.target/sharp-linux-x64/src/common.o] Error 1[0m
                                 [0mgyp ERR! build error [0m
                                 [0mgyp ERR! stack Error: `make` failed with exit code: 2[0m
                                 [0mgyp ERR! stack     at ChildProcess.onExit (/root/.nvm/versions/node/v14.19.0/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:194:23)[0m
                                 [0mgyp ERR! stack     at ChildProcess.emit (events.js:400:28)[0m
                                 [0mgyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:282:12)[0m
                                 [0mgyp ERR! System Linux 4.14.246-187.474.amzn2.x86_64[0m
                                 [0mgyp ERR! command "/root/.nvm/versions/node/v14.19.0/bin/node" "/root/.nvm/versions/node/v14.19.0/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"[0m
                                 [0mgyp ERR! cwd /codebuild/output/src927006233/src/create-react-app-auth-amplify/amplify/backend/function/S3Trigger71b5b76d/src/node_modules/sharp[0m
                                 [0mgyp ERR! node -v v14.19.0[0m
                                 [0mgyp ERR! node-gyp -v v5.1.0[0m
                                 [0mgyp ERR! not ok [0m
                                 [0mnpm WARN S3Trigger71b5b76d@2.0.0 No repository field.[0m

Running amplify push from my windows box packages up the lambda and deploys it working totally fine, the problem is only when it pulls from github and tries to run through the amplify build platform. I am running node v 16

Thanks for any ideas! My google-foo is failing me this time.

Turner
  • 33
  • 4

3 Answers3

5

According to the installation guide for AWS Lambda you can add the following command as the preinstall script to the package.json of your Amplify lambda:

{
  "name": "file-storage-get-s3-signed-link",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "preinstall": "SHARP_IGNORE_GLOBAL_LIBVIPS=1 npm install --arch=x64 --platform=linux --libc=glibc sharp"
  }
}
Aleksandr Sakharov
  • 1,272
  • 1
  • 11
  • 10
2

I have the exact same issue. To make it work on lambda with amplify push, I had to run the following in the backend/function/function_name/src folder npm install --arch=x64 --platform=linux sharp. But when pushing to git, the triggered deployment fails the same way.

Edit

Solved First I tried installing install vips with yum in the prebuild stage (reference). But I got a new error:

[0m/usr/local/include/vips/vips8:35:10: fatal error: glib-object.h: No such file or directory[0m
[0m #include <glib-object.h>[0m
[0m          ^~~~~~~~~~~~~~~[0m
[0mcompilation terminated.[0m

So then I tried downloading, make and install vips from the git source (reference). Was still getting the same error. But after installing gtk-doc it worked... almost. The build did not fail, but the lambda function did not work because sharp was not found, there's a fix on the sharp docs(reference). So I add a script in the package.json. Here's the code used

Build file amplify.yml

version: 1
backend:
  phases:
    preBuild:
      commands:
        - '# Install gtk-doc; download, make and install vips'
        - yum -y install gtk-doc
        - wget https://github.com/libvips/libvips/releases/download/v8.12.2/vips-8.12.2.tar.gz
        - tar -xzvf vips-8.12.2.tar.gz
        - cd vips-8.12.2
        - ./configure
        - make
        - make install
        - cd ..
    build:
      commands:
        - '# Execute Amplify CLI with the helper script'
        - amplifyPush --simple
frontend:
  phases:
    preBuild:
      commands:
        - npm ci
    build:
      commands:
        - npm run build
  artifacts:
    baseDirectory: .next
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*

package.json

"scripts": {
    "amplify:<LambdaFunctionName>": "cd amplify/backend/function/<LambdaFunctionName>/src && npm install && rm -rf node_modules/sharp && SHARP_IGNORE_GLOBAL_LIBVIPS=1 npm install --arch=x64 --platform=linux --libc=glibc sharp && cd -"
  },

After like 15+ failed builds, it finally worked!!! Hope this helps, Happy coding!

  • Yeah. I still haven't found a solution. I ended up commenting out the entire lambda except the handler call, git pushing, then uncommenting it and running `amplify push` after the CI build succeeded. I would love to find a solution though. – Turner Jun 02 '22 at 17:11
  • Wow thanks for updating! This looks like a much better solution. I had been just been commenting sharp out and running `amplify push` from my desktop after it builds. Quick q, does that package.json file refer to the one in your project's base directory or the lambda's? I assume it's the base one due to the script `cd` ing into the lambda. – Turner Jun 12 '22 at 00:28
  • Yes, the project package.json file, not the lambda function. The ''should be replaced with your function name as well as the cd into the folder. In my case it is ```"amplify:S3Trigger81b11d96": "cd amplify/backend/function/S3Trigger81b11d96/src && npm install && rm -rf node_modules/sharp && SHARP_IGNORE_GLOBAL_LIBVIPS=1 npm install --arch=x64 --platform=linux --libc=glibc sharp && cd -"``` – Raphael Kottakal Jun 12 '22 at 16:14
0

If you deploy amplify app not just from amplify-cli but from ci/cd as well, solution in approved answer will not work during automatic deploy, in case build for lambda is produced inside docker image. So solution ugly but working for this case is:

Put in package.json:

 "resolutions": {
    "minipass": "2.7.0"
  },

https://github.com/lovell/sharp/issues/1882#issuecomment-534266128

Namli
  • 31
  • 4