Why doesn't sharp module work by itself in lambda
Andriy-Kulak opened this issue · comments
I have two questions.
So I am relatively new to lambda and I was wondering why I couldn't get sharp npm package to work on a deployed lambda function. It only works locally if I test the endpoint on my computer with serverless-offline. I looked further into it and looks like Sharp package has prerequisites (gyp, python, etc) that I assume are not installed in the deployed lambda instance:
I don't believe lamda has the node-gyp and python installed by itself if I use Node as the environment.
-
So is this what this package supposed to do - prepackage sharp dependencies so it works in deployed lambda?
-
How would I go about prepackaging these dependencies myself in a serverless framework if I want to use the sharp npm package by itself? I would like to learn this for current and feature endeavors.
Prerequisites: (link here)
- Node v4+
- C++11 compatible compiler such as gcc 4.8+, clang 3.0+ or MSVC 2013+
- node-gyp and its dependencies (includes Python)
I got my sharp npm packge instance to work after using your tarball. Thanks for the work!!! :-)
Hi @Andriy-Kulak. Your understanding is correct.
The sharp
package contains bindings to libvips which are developed with C++, so when you do npm install sharp
, npm uses node-gyp to setup the build environment and then compile the C++ bindings within the context of your OS. sharp
is a Node.js native addon. This means that, if you npm install sharp
on MacOS, the binary that's produced won't run on Windows, or Linux. That's why serverless-offline
would work, but not when you deploy—serverless-offline
is probably running on the same OS where you installed sharp
.
For convenience, and so that you can deploy from any OS, serverless-sharp-image
includes some extra "sugar" to package a version of sharp
which was created with npm install sharp
on an EC2 instance which resembles the Lambda Execution Environment (i.e. it'll run on Lambda.) It also includes some Linux .so files (think .dll on Windows) which don't exist on Lambda that are required by libvips. If you're interested in how that's done, take a look at the README in ~/lib. Additionally, there's some webpack configuration which makes sure the right sharp package gets bundled when you deploy.