Skip to main content

Building node-canvas in Amazon Linux AMI for Lambdas

2 min read

Building node-canvas for use inside an AWS Lambda is tricky as it requires some dependencies to be built by node-gyp against the target architecture. If you build these dependencies locally before deploying your lambda, they'll be built on against your computer's architecture.

Amazon Lambdas run Amazon Linux AMI instances. Luckily this distro is available in docker the docker hub (https://hub.docker.com/_/amazonlinux/)

You'll need to mount your app into this docker instance, build all the dependencies then deploy you app to Lambda.

Follow the steps ahead:

Run up docker in interactive mode and mount your project directory into it.

$ docker run -it  -v $PWD:/opt/app  amazonlinux /bin/bash

Install nvm

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.0/install.sh | bash

Set up NVM and install the node version you want in your lambda

. ~/.nvm/nvm.sh
nvm install 6.10.0

Install the node-canvas dependencies

yum install cairo-devel libjpeg-turbo-devel giflib-devel -y

Install the development tools with yum, this will give you g++ to run the make files needed to build with node-gyp

yum groupinstall 'Development Tools'

Install node-gyp and node-pre-gyp

npm i -g node-gyp

Install node-canvas

npm i canvas

And you're done! This should get you round most of the issues when building node-canvas for lambdas.

All that's left to do is deploy to lambda, set up your API Gateway and off you go!

© 2021 by Madole.
GitHub Repository
Last build: 1731364498764