Search code examples
lambdaaws-lambdaserverless-frameworkserverless-architecturesharp

Packages compiling native code (npm install) using AWS Lambda


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?


Solution

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