4

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?

Emi
  • 4,597
  • 2
  • 31
  • 34

2 Answers2

8

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.

Alex
  • 8,321
  • 1
  • 34
  • 30
  • I've never used docker and so I'm a bit lost. Is this command supposed to install node_modules compiled in a linux virtual machine overwriting the current Windows ones I have? Then I can do a "sls deploy" as usually? – Emi Aug 08 '17 at 22:06
  • 1
    @Emi Yep! That's right. You'll spin up a Docker container (which is similar to a Linux virtual machine) and run "npm install". The "-v" command mounts your local volume to a volume in the container, so you can share data. This way, your container will be able to your `package.json` so it knows which packages to install, and the installed packages in `node_modules` will be available in your directory. Note: I haven't done Docker on Windows so the volume stuff could be tricky. – Alex Aug 08 '17 at 23:38
  • Thanks! I'm getting an error when running this command: "Error: EACCES: permission denied, mkdir '/var/task/node_modules'" and "Please try running this command again as root/Administrator.". Should I add anything else to this command? – Emi Aug 08 '17 at 23:47
  • 1
    You may need to switch how you mount since you're using Windows. Are you using Windows Command Line or Powershell? [This answer](https://stackoverflow.com/a/41489151/3457661) has some help. You'll need to switch the volume mounting portion (`-v "$PWD":/var/task`) to either `-v %cd%:/var/task` (for Windows Command Line) or (`-v ${PWD}:/var/task`) for Powershell. – Alex Aug 09 '17 at 00:19
1

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.

Popoi Menenet
  • 1,018
  • 9
  • 20
  • Have you implemented that using serverless framework? I'm not sure what would be the best way to do this. – Emi Aug 08 '17 at 12:24