Skip to content

Instantly share code, notes, and snippets.

@mhart
Last active August 26, 2019 20:52
Show Gist options
  • Save mhart/10517850dd36d577364ecb1530a73654 to your computer and use it in GitHub Desktop.
Save mhart/10517850dd36d577364ecb1530a73654 to your computer and use it in GitHub Desktop.
Installing node-canvas for Lambda using docker-lambda

Installing node-canvas for Lambda using docker-lambda

Assumes your Lambda handler (eg index.js, app.js, etc) is in the current directory and you've already run npm install locally (ie, you have ./node_modules already) – the OS you ran it on shouldn't matter.

nodejs8.10 and nodejs10.x

This will reinstall any native module binaries, with our current directory mounted as /var/task, which mirrors where it mounts in Lambda

docker run --rm -v "$PWD":/var/task lambci/lambda:build-nodejs8.10 npm rebuild --update-binary
# OR
docker run --rm -v "$PWD":/var/task lambci/lambda:build-nodejs10.x npm rebuild --update-binary

Extra step for nodejs10.x only

Instructions differ for nodejs10.x because node-canvas has some dependencies that don't exist there, because it's running on (a very slim) Amazon Linux 2

mkdir ./lib
docker run --rm -v "$PWD":/var/task lambci/lambda:build-nodejs10.x \
  cp /usr/lib64/libmount.so.1 /usr/lib64/libblkid.so.1 /usr/lib64/libuuid.so.1 \
  /var/task/lib/

Testing

You can then test that it works (assumes a handler() function in index.js):

docker run --rm -v "$PWD":/var/task lambci/lambda:nodejs8.10 index.handler
# OR
docker run --rm -v "$PWD":/var/task lambci/lambda:nodejs10.x index.handler

You should then be able to zip up the local directory and deploy to your Lambda!

You can bundle up any of these dependencies as a layer too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment