Search code examples
node.jsaws-lambdaaws-amplifysharp

Sharp on AWS Amplify Backend Lambda Causing Build to Fail


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 [email protected] 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.


Solution

  • 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!