Lambda@Edge origin function, libvips-cpp.so.42: invalid ELF header
Okay so I am creating this because I've already gone through every last post about this and nothing is fixing my problem. I've attempted to get this running both in Windows 10 as well as the Ubuntu 18 WSL using Docker but neither option seems to do it.
So like some people I am trying to get Sharp working in Lambda@Edge and I am encountering the same error as many people in a NodeJS 12 runtime:
"errorType": "Error",
"errorMessage": "\nSomething went wrong installing the \"sharp\" module\n\n/var/task/node_modules/sharp/build/Release/../../vendor/lib/libvips-cpp.so.42: invalid ELF header\n\n- Ensure \"linux\" is used at install time as well as runtime\n- Consult the installation documentation at https://sharp.pixelplumbing.com/install\n- Search for this error at https://github.com/lovell/sharp/issues\n",
"trace": [
"Error: ",
"Something went wrong installing the \"sharp\" module",
"",
"/var/task/node_modules/sharp/build/Release/../../vendor/lib/libvips-cpp.so.42: invalid ELF header",
"",
"- Ensure \"linux\" is used at install time as well as runtime",
"- Consult the installation documentation at https://sharp.pixelplumbing.com/install",
"- Search for this error at https://github.com/lovell/sharp/issues",
"",
" at Object.<anonymous> (/var/task/node_modules/sharp/lib/constructor.js:34:9)",
" at Module._compile (internal/modules/cjs/loader.js:1158:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)",
" at Module.load (internal/modules/cjs/loader.js:1002:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:901:14)",
" at Module.require (internal/modules/cjs/loader.js:1044:19)",
" at require (internal/modules/cjs/helpers.js:77:18)",
" at Object.<anonymous> (/var/task/node_modules/sharp/lib/index.js:3:15)",
" at Module._compile (internal/modules/cjs/loader.js:1158:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)"
]
}
I tried installing the sharp module into my project using the recommended command:
docker run --rm --volume ${PWD}/lambda/origin-response-function:/var/task lambci/lambda:build-nodejs12.x npm install sharp
Which I noted in the output, it was downloading the appropriate vips (linux x64):
> [email protected] install /var/task/node_modules/sharp
> (node install/libvips && node install/dll-copy && prebuild-install --runtime=napi) || (node-gyp rebuild && node install/dll-copy)
info sharp Downloading https://github.com/lovell/sharp-libvips/releases/download/v8.9.1/libvips-8.9.1-linux-x64.tar.gz
Which should be the pre-compiled binaries. So I tried it and got the same "invalid ELF header" error like I mentioned at the start of my post.
Then I thought to try and build it from source with by adding --build-from-source to my npm install command:
docker run --rm --volume ${PWD}/lambda/origin-response-function:/var/task lambci/lambda:build-nodejs12.x npm install sharp --build-from-source
In case something needed to be recompiled but still got the same failure.
At this point, short of firing up an EC2 instance and trying it that way, I'm hoping that this is just something I am overlooking or misunderstanding. Here's the output of my npm install command with the --verbose flag set:
> [email protected] install /var/task/node_modules/sharp
> (node install/libvips && node install/dll-copy && prebuild-install --runtime=napi) || (node-gyp rebuild && node install/dll-copy)
info sharp Downloading https://github.com/lovell/sharp-libvips/releases/download/v8.9.1/libvips-8.9.1-linux-x64.tar.gz
prebuild-install info begin Prebuild-install version 5.3.3
prebuild-install info looking for cached prebuild @ /root/.npm/_prebuilds/ba3ce8-sharp-v0.25.2-napi-v3-linux-x64.tar.gz
prebuild-install http request GET https://github.com/lovell/sharp/releases/download/v0.25.2/sharp-v0.25.2-napi-v3-linux-x64.tar.gz
prebuild-install http 200 https://github.com/lovell/sharp/releases/download/v0.25.2/sharp-v0.25.2-napi-v3-linux-x64.tar.gz
prebuild-install info downloading to @ /root/.npm/_prebuilds/ba3ce8-sharp-v0.25.2-napi-v3-linux-x64.tar.gz.41-46a3de039fe1d.tmp
prebuild-install info renaming to @ /root/.npm/_prebuilds/ba3ce8-sharp-v0.25.2-napi-v3-linux-x64.tar.gz
prebuild-install info unpacking @ /root/.npm/_prebuilds/ba3ce8-sharp-v0.25.2-napi-v3-linux-x64.tar.gz
prebuild-install info unpack resolved to /var/task/node_modules/sharp/build/Release/sharp.node
prebuild-install info install Successfully installed prebuilt binary!
npm verb lifecycle [email protected]~install: unsafe-perm in lifecycle false
npm verb lifecycle [email protected]~install: PATH: /var/lang/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/var/task/node_modules/sharp/node_modules/.bin:/var/task/node_modules/.bin:/var/lang/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin
npm verb lifecycle [email protected]~install: CWD: /var/task/node_modules/sharp
npm timing action:install Completed in 6158ms
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm timing action:postinstall Completed in 32ms
npm verb unlock done using /root/.npm/_locks/staging-1313e7c4addbfc02.lock for /var/task/node_modules/.staging
npm timing stage:executeActions Completed in 9493ms
npm timing stage:rollbackFailedOptional Completed in 0ms
npm timing stage:runTopLevelLifecycles Completed in 13694ms
npm WARN saveError ENOENT: no such file or directory, open '/var/task/package.json'
npm info lifecycle undefined~preshrinkwrap: undefined
npm info lifecycle undefined~shrinkwrap: undefined
npm notice created a lockfile as package-lock.json. You should commit this file.
npm info lifecycle undefined~postshrinkwrap: undefined
npm WARN enoent ENOENT: no such file or directory, open '/var/task/package.json'
npm verb enoent This is related to npm not being able to find a file.
npm verb enoent
npm WARN task No description
npm WARN task No repository field.
npm WARN task No README data
npm WARN task No license field.
+ [email protected]
added 75 packages from 154 contributors and audited 163 packages in 13.884s
2 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
npm verb exit [ 0, true ]
npm timing npm Completed in 14220ms
npm info ok
Is there something I'm not doing correctly?
Hi, did you see #787 and #2073?
Hey, thanks for the response.
I did encounter these when reviewing the Issues list, but in my case I am using the function as an origin response, and it's overall size is only about 12.7MB so it fits in there just fine.
Are you able to provide a minimal, ready-to-deploy zip file that generates this error?
Okay I am back to report that I have resolved the issue, and it had nothing to do with the installation of Sharp at all.
It turns out, that using the zip command in WSL does not include a lot of files in node_modules/sharp. I think this is because I was using a directory that was on a Windows file system. I noticed this because when I originally created this issue, the zipped project size was only about 13MB, which I guess is much smaller than it should be.
So to fix this in my case, I started with the sharp installation docker command provided here: https://sharp.pixelplumbing.com/install#aws-lambda
And modified it to include a second volume for zip output so I could execute the zip command within the build container and output it to a local dist folder:
docker run -v ${PWD}/dist:/dist -v ${PWD}/lambda/origin-response-function:/var/task lambci/lambda:build-nodejs12.x /bin/bash -c "npm install sharp && zip -FS -q -r /dist/origin-response-function.zip *"
This produced a zip file that was about 33MB in size, so much larger than when I was using the zip command directly in WSL (~13MB). So with that, I uploaded my project to Lambda and it ran just fine.
I appreciate you taking the time to help me, but file this one as a 'my bad'...