Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 492
Emi Avatar asked Jan 03 '23 15:01

Emi


2 Answers

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.

like image 139
Alex Avatar answered Jan 13 '23 23:01

Alex


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.

like image 34
Popoi Menenet Avatar answered Jan 13 '23 23:01

Popoi Menenet