I need to use the sharp package to resize images in a Lambda function, but it builds the native code when doing an “npm install” in my Windows machine, which definitely won’t work in Amazon Linux server where Lambda is hosted.
What’s the recommended way to solve this when using serverless?
If you want to integrate more cleanly with the Serverless Framework, you could install your NPM packages inside a Docker container that's mounted to your working directory:
For Node v6.10:
$ docker run -v "$PWD":/var/task lambci/lambda:build-nodejs6.10 npm install
For Node v4.3:
$ docker run -v "$PWD":/var/task lambci/lambda:build-nodejs4.3 npm install
This will install all the packages in your package.json
and mount the node_modules/
in your directory.
This is using a Docker container from Lambci, which is very close to the actual AWS Lambda environment.
I had similar issue when developing NodeJS image manipulation application for Lambda in my Windows machine. I managed to resolve the issue by using Docker.
Since AWS Lambda underlying execution environment is based on Amazon Linux image, in which the image is made public by AWS for Docker, then you can actually pull the image and run the Amazon Linux container in your Windows machine.
So in the container, I had my code cloned in there, run the npm install
, zip and upload them into S3 bucket, and finally create/update the Lambda's code from the S3.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With