Sharp on AWS Amplify Backend Lambda导致构建失败



我对aws amplify非常陌生,并且有一个amplify应用程序,该应用程序有一个后端lambda函数,该函数使用sharp作为依赖项。当我推到git来触发部署时,我的构建失败了,我认为是由于vips/vips8没有找到一个尖锐的依赖项。

相关日志

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

从我的windows盒子运行amplify push包lambda和部署它工作完全正常,问题是只有当它从github拉,并试图通过放大构建平台运行。我正在运行节点v16

谢谢你的建议!这次我的google-foo失败了。

根据AWS Lambda的安装指南,您可以将以下命令作为preinstall脚本添加到包中。你的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"
}
}

我也有同样的问题。为了使它在lambda上使用amplify push,我必须在backend/function/function_name/src文件夹npm install --arch=x64 --platform=linux sharp中运行以下代码。但是当推送到git时,触发的部署会以同样的方式失败。

编辑

首先,我尝试在预构建阶段安装yum的install vips(参考)。但是我得到了一个新的错误:

[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

所以我试着从git源代码(参考)下载,制作和安装vips。还是得到同样的错误。但是在安装gtk-doc之后,它工作了…几乎。构建没有失败,但是lambda函数没有工作,因为没有找到sharp,有一个关于sharp文档的修复(参考)。所以我在package.json中添加了一个脚本。下面是使用的代码

构建文件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 -"
},

在15+失败的构建之后,它终于工作了!!希望这有帮助,快乐编码!

如果您不仅从amplify-cli而且从ci/cd部署amplify应用程序,那么在自动部署期间,如果在docker映像中生成lambda构建,则批准答案中的解决方案将无法工作。所以解决方案很难看,但在这种情况下是:

Put in package.json:

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

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

最新更新