You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm a Linux & node noob. I'm trying to run FabricJS (which requires node-canvas) in AWS Lambda. I've been able to follow the instructions to get up and running on an AWS Linux EC2, however, Lambda has me at my wits end. Anyone have any tips or pointers on how to get this compiled for AW Lambda?
This is actually a use case for me as well. I hope to try this this weekend and will let you know what I find. I think you'll have to bundle the SOs and change bindings.gyp to point to them.
This is actually a use case for me as well. I hope to try this this
weekend and will let you know what I find. I think you'll have to bundle
the SOs and change bindings.gyp to point to them.
—
Reply to this email directly or view it on GitHub #680 (comment)
.
Didn't find time this weekend, and not sure when I'll get to this. If you want to try this on your own, you might check out the commits relating to the third bullet point here: #571
I was able to compile node-canvas and get it running on lambda by building dynamic and using rpath. Since lambda always runs in the same /var/task this makes it:
You may want to set the prefix= ~/canvas to have all the files in one place.
Install node-canvas with npm
cd node_modules/canvas; node-gyp rebuild
mkdir /pkg and cp the .so files (/canvas/lib/*.so) there, using -L to dereference symlinks.
scp the pkg directory to the local lambda folder, possibly putting the files in the right places. (.so in root, node_modules/canvas with other libs). You'll probably want to rearrange this.
Good luck!
Reacted by SangHee Kim, Tomas, Nathan Vander Wilt, Aman Chopra and Fawaz Alharbi
@mankins Can you provide more details about the process?
I tried to follow your steps, but I think I miss something, because I am not able to make it work.
I got this error in AWS lambda:
{
"errorMessage": "libpng16.so.16: cannot open shared object file: No such file or directory",
"errorType": "Error",
"stackTrace": [
"Module.load (module.js:356:32)",
"Function.Module._load (module.js:312:12)",
"Module.require (module.js:364:17)",
"require (module.js:380:17)",
"Object. (/var/task/node_modules/canvas/lib/bindings.js:2:18)",
"Module._compile (module.js:456:26)",
"Object.Module._extensions..js (module.js:474:10)",
"Module.load (module.js:356:32)",
"Function.Module._load (module.js:312:12)",
"Module.require (module.js:364:17)"
]
}
My file structure (the zip that I upload to AWS Lambda) has the next structure:
Canvas.js
node_modules folder
pkg folder (with the *.so items, including libpng16.so.16)
In the AWS AMI instance that I am using to generate the zip file (same than you provide in your link), if I go to canvasexample/node_modules/canvas, and execute
That I guess is not correct, because (If I understand), libpng16.so.16 and the other dependencies should point to my pkg folder (may be something like "/var/task/pkg/libpng16.so.16") , that is the same I will upload to Lambda, and same that would be executed, right? (I also tried to follow another similar issue in Package pre-compiled binaries with node-canvas for downstream consumers #641.
It sounds like you need to use the -rpath=/var/task/ of -rpath=/var/task/pkg ... you're basically baking in a search path for dynamic libs (.so) and so if you put them in pkg, you'll need to compile them that way.
I followed the process outlined by @mankins and created a GitHub repo with the compiled shared object (see src/*.so.*) files and an example with this working. Hope someone else finds this useful.
Reacted by Andrew Steadman, Aaron, D Morty k, Harrison Powers and Jonah Werre
Create an AWS EC2 Instance (you can use the AWS Web Console) t2.micro based on the AMI from the link above
SSH into it (you need the pem you created with it)
Run the following commands, I suggest 1 by 1 to know if an error happened
export LDFLAGS=-Wl,-rpath=/var/task/
sudo yum groupinstall "Development Tools" -y
sudo yum install fontconfig-devel.x86_64 -y
sudo yum erase cairo -y
export PKG_CONFIG_PATH='/usr/local/lib/pkgconfig'
export LD_LIBRARY_PATH='/usr/local/lib':$LD_LIBRARY_PATH
sudo yum -y install zlib-devel
curl -L http://downloads.sourceforge.net/libpng/libpng-1.6.21.tar.xz -o libpng-1.6.21.tar.xz
tar -Jxf libpng-1.6.21.tar.xz && cd libpng-1.6.21
./configure --prefix=/home/ec2-user/canvas
make
sudo make install
cd ..
curl http://www.ijg.org/files/jpegsrc.v8d.tar.gz -o jpegsrc.tar.gz
tar -zxf jpegsrc.tar.gz && cd jpeg-8d/
./configure --disable-dependency-tracking --prefix=/home/ec2-user/canvas
make
sudo make install
cd ..
curl -L http://www.cairographics.org/releases/pixman-0.34.0.tar.gz -o pixman-0.34.0.tar.gz
tar -zxf pixman-0.34.0.tar.gz && cd pixman-0.34.0/
./configure --prefix=/home/ec2-user/canvas
make
sudo make install
cd ..
curl -L http://download.savannah.gnu.org/releases/freetype/freetype-2.6.tar.gz -o freetype-2.6.tar.gz
tar -zxf freetype-2.6.tar.gz && cd freetype-2.6/
./configure --prefix=/home/ec2-user/canvas
make
sudo make install
cd ..
curl https://www.freedesktop.org/software/fontconfig/release/fontconfig-2.12.0.tar.bz2 -o fontconfig.gz
sudo yum install libpng-devel -y
curl -L http://cairographics.org/releases/cairo-1.14.6.tar.xz -o cairo-1.14.6.tar.xz
tar -Jxf cairo-1.14.6.tar.xz && cd cairo-1.14.6
PKG_CONFIG_PATH=/home/ec2-user/canvas/lib/pkgconfig
PKG_CONFIG=/home/ec2-user/canvas/lib/pkgconfig
./configure --disable-dependency-tracking --without-x --prefix=/home/ec2-user/canvas
make
sudo make install
cd ..
sudo yum install giflib-devel -y
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.4/install.sh | bash
#After installing NVM you'll have to close your ssh session and reconnect.
nvm install 6.10
PKG_CONFIG_PATH=/home/ec2-user/canvas/lib/pkgconfig:/usr/local/lib/pkgconfig
# Gave up moment
sudo yum install cairo cairo-devel cairomm-devel libjpeg-turbo-devel pango pango-devel pangomm pangomm-devel giflib-devel -y
# Copy the pango dependencies
cp /usr/lib64/libpango*.so.* /home/ec2-user/canvas/lib/
cp /usr/lib64/libpango*.so /home/ec2-user/canvas/lib/
After that, you can npm install node-canvas into the ec2, and copy over SCP the folders in node_modules (should just be this library and the nan lib)
Copy that to your local node_modules before creating the zip (so whatever gets uploaded to Lambda, has these specially built modules)
I don't really understand why using "-rpath" and rebuilding the libraries from source should be required. According to the Lambda Documentation, LD_LIBRARY_PATH includes LAMBDA_TASK_ROOT (/var/task) and LAMBDA_TASK_ROOT/lib (/var/task/lib). So we should just be able to put libraries in there and have them found automatically.
I took the Lambda AMI and spun up a VM. I installed Node and development tools:
sudo yum groupinstall "Development Tools" -y
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.4/install.sh | bash
# Exit session and reopen here to make nvm available
nvm install 6.10
Then I installed node-canvas as you normally would:
let{createCanvas}=require("canvas");functionhello(event,context,callback){letcanvas=createCanvas(200,200),ctx=canvas.getContext('2d');// Write "Awesome!"ctx.font='30px Impact';ctx.rotate(0.1);ctx.fillText('Awesome!',50,100);// Draw line under textlettext=ctx.measureText('Awesome!');ctx.strokeStyle='rgba(0,0,0,0.5)';ctx.beginPath();ctx.lineTo(50,102);ctx.lineTo(50+text.width,102);ctx.stroke();callback(null,'<img src="'+canvas.toDataURL()+'" />');}module.exports={hello};
And a serverless.yml file to deploy it to Amazon for me:
I downloaded this entire folder to my local machine. Then I called serverless deploy to deploy it to Lambda, and serverless invoke --function hello and I got the successful result from it running on Lambda:
Reacted by Tomas, Evan Rubinton and Tibor Otott-Kovacs
Canvas 2.0 and 1.6 works out-of-the-box on AWS Lambda thanks to prebuilds. However, you must build your Lambda ZIP file on Linux (or a Linux Docker container) so that the correct prebuilt binary is included.
Is it still the case that this works "out-of-the-box" I'm getting the following Lambda error:
{"errorType": "Error","errorMessage": "libuuid.so.1: cannot open shared object file: No such file or directory","trace": ["Error: libuuid.so.1: cannot open shared object file: No such file or directory"," at Object.Module._extensions..node (internal/modules/cjs/loader.js:730:18)"," at Module.load (internal/modules/cjs/loader.js:600:32)"," at tryModuleLoad (internal/modules/cjs/loader.js:539:12)"," at Function.Module._load (internal/modules/cjs/loader.js:531:3)"," at Module.require (internal/modules/cjs/loader.js:637:17)"," at require (internal/modules/cjs/helpers.js:22:18)"," at Object.<anonymous> (/var/task/node_modules/canvas/lib/bindings.js:3:18)"," at Module._compile (internal/modules/cjs/loader.js:701:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)"," at Module.load (internal/modules/cjs/loader.js:600:32)"," at tryModuleLoad (internal/modules/cjs/loader.js:539:12)"," at Function.Module._load (internal/modules/cjs/loader.js:531:3)"," at Module.require (internal/modules/cjs/loader.js:637:17)"," at require (internal/modules/cjs/helpers.js:22:18)"," at Object.<anonymous> (/var/task/node_modules/canvas/lib/canvas.js:9:18)"," at Module._compile (internal/modules/cjs/loader.js:701:30)"]}
mkdir test && cd test
npm install canvas@2.5.0 --save
5. Create Lambda handler index.js
let{createCanvas}=require("canvas");functionhello(event,context,callback){letcanvas=createCanvas(200,200),ctx=canvas.getContext('2d');// Write "Awesome!"ctx.font='30px Impact';ctx.rotate(0.1);ctx.fillText('Awesome!',50,100);// Draw line under textlettext=ctx.measureText('Awesome!');ctx.strokeStyle='rgba(0,0,0,0.5)';ctx.beginPath();ctx.lineTo(50,102);ctx.lineTo(50+text.width,102);ctx.stroke();callback(null,'<img src="'+canvas.toDataURL()+'" />');}module.exports={hello};
For some reason the /lib64 directory on Lambda for Amazon Linux 2 (Node 10) is incredibly sparse, much more empty than in the corresponding AMI they've published, which doesn't make sense to me.
I had to bundle the following libraries from Amazon Linux 2 with my function:
What is that size of all that? Copying all these files into your project will put you dangerously close the the max size of a Lambda function (250 MB).
Activity
zbjornson commentedon Nov 26, 2015
This is actually a use case for me as well. I hope to try this this weekend and will let you know what I find. I think you'll have to bundle the SOs and change bindings.gyp to point to them.
schaq8 commentedon Nov 26, 2015
Zach, I'd be so grateful!
On Thu, Nov 26, 2015 at 12:20 AM, Zach Bjornson notifications@github.com
wrote:
zbjornson commentedon Dec 1, 2015
Didn't find time this weekend, and not sure when I'll get to this. If you want to try this on your own, you might check out the commits relating to the third bullet point here: #571
zomgbre commentedon Dec 3, 2015
+1 I'm also interested in this.
zbjornson commentedon Dec 4, 2015
Also related: #641 (although they're not statically linking). You might find some good stuff in the forks from the people commenting there and in 571.
ssomai commentedon Dec 4, 2015
+1 I'm also interested in this.
mankins commentedon Jan 17, 2016
I was able to compile node-canvas and get it running on lambda by building dynamic and using rpath. Since lambda always runs in the same /var/task this makes it:
and then build normally.
You should also make sure you're compiling on the same AMI environment that lambda currently uses: http://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html
jalieven commentedon Feb 4, 2016
@mankins could you provide some more info on how you dynamically build node-canvas?
mankins commentedon Feb 4, 2016
Hey @jalieven here's some notes I made:
http://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html
We then want to build our libraries using an "rpath":
You may want to set the prefix= ~/canvas to have all the files in one place.
/pkg and cp the .so files (/canvas/lib/*.so) there, using -L to dereference symlinks.Good luck!
jalieven commentedon Feb 10, 2016
@mankins many thanks for your notes. Works like a charm!
mauricio-trigueros commentedon Feb 10, 2016
@mankins Can you provide more details about the process?
I tried to follow your steps, but I think I miss something, because I am not able to make it work.
I got this error in AWS lambda:
My file structure (the zip that I upload to AWS Lambda) has the next structure:
In the AWS AMI instance that I am using to generate the zip file (same than you provide in your link), if I go to canvasexample/node_modules/canvas, and execute
ldd build/Release/canvas.nodeThe result that I get is:
That I guess is not correct, because (If I understand), libpng16.so.16 and the other dependencies should point to my pkg folder (may be something like "/var/task/pkg/libpng16.so.16") , that is the same I will upload to Lambda, and same that would be executed, right? (I also tried to follow another similar issue in Package pre-compiled binaries with node-canvas for downstream consumers #641.
Any help is very very welcome!
Thank you very much!
mankins commentedon Feb 10, 2016
It sounds like you need to use the -rpath=/var/task/ of -rpath=/var/task/pkg ... you're basically baking in a search path for dynamic libs (.so) and so if you put them in pkg, you'll need to compile them that way.
mauricio-trigueros commentedon Feb 11, 2016
Thanks! I arranged to make it work in the way you were describing.
I will continue playing with this "rpath" option!
Thanks again!
willbamford commentedon Feb 23, 2016
I followed the process outlined by @mankins and created a GitHub repo with the compiled shared object (see
src/*.so.*) files and an example with this working. Hope someone else finds this useful.navihtot commentedon Apr 4, 2016
Following @webseed notes I managed to get camanJS library working on AWS Lambda so here it it helps somebody.
11 remaining items
tomasdev commentedon Jan 9, 2018
jgautheron commentedon Jan 9, 2018
Basically the text is rendered as blocks, as if the font was not found.
tomasdev commentedon Jan 9, 2018
tomasdev commentedon Feb 4, 2018
By the way, in case anyone needs it, here's the full list of commands I ran in a Linux AMI (the one that https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html shows, currently 6.10)
Steps to build node-canvas for Lambda usage
After that, you can
npm install node-canvasinto the ec2, and copy over SCP the folders innode_modules(should just be this library and thenanlib)Copy that to your local node_modules before creating the zip (so whatever gets uploaded to Lambda, has these specially built modules)
thenickdude commentedon Feb 10, 2018
I don't really understand why using "-rpath" and rebuilding the libraries from source should be required. According to the Lambda Documentation,
LD_LIBRARY_PATHincludesLAMBDA_TASK_ROOT(/var/task) andLAMBDA_TASK_ROOT/lib(/var/task/lib). So we should just be able to put libraries in there and have them found automatically.I took the Lambda AMI and spun up a VM. I installed Node and development tools:
Then I installed node-canvas as you normally would:
Then I copied the system libraries from
/usr/lib64/into a./lib/subdirectory in my Node project:I added an index.js to test Canvas with:
And a
serverless.ymlfile to deploy it to Amazon for me:The resulting project structure:
I downloaded this entire folder to my local machine. Then I called
serverless deployto deploy it to Lambda, andserverless invoke --function helloand I got the successful result from it running on Lambda:tomasdev commentedon Feb 11, 2018
We could technically close this ticket and just leave that last comment as instructions for use in Lambda.
I went for the manual build because I didn't know where to take the .so and libs from. That is way easier, thanks @thenickdude
thenickdude commentedon Feb 12, 2018
I've added those instructions to the Wiki now:
https://github.com/Automattic/node-canvas/wiki/Installation---AWS-Lambda
zbjornson commentedon Feb 18, 2018
Closing since @thenickdude added instructions (thank you!).
natevw commentedon Oct 25, 2018
Updated link is https://github.com/Automattic/node-canvas/wiki/Installation%3A-AWS-Lambda, which also includes the encouraging note:
and cross-references #1231
jwerre commentedon Jun 7, 2019
Is it still the case that this works "out-of-the-box" I'm getting the following Lambda error:
Here's how to reproduce it:
1. Spin up an Amazon Linux 2 AMI:
I installed this one. amzn2-ami-hvm-2.0.20190313-x86_64-gp2. Then connect to the server over ssh.
2. Install node 10
3. Install Serverless
4. Create project directory and install Canvas
5. Create Lambda handler
index.js6. Create
serverless.yml7. Deploy the function
8. Invoke the function
thenickdude commentedon Jun 7, 2019
For some reason the /lib64 directory on Lambda for Amazon Linux 2 (Node 10) is incredibly sparse, much more empty than in the corresponding AMI they've published, which doesn't make sense to me.
I had to bundle the following libraries from Amazon Linux 2 with my function:
Notably, libuuid is not even included in the prebuilt Canvas. Is the Amazon Linux 2 image currently deployed on Lambda actually broken?
jwerre commentedon Jun 7, 2019
@thenickdude libuuid is included on my AMI
What is that size of all that? Copying all these files into your project will put you dangerously close the the max size of a Lambda function (250 MB).
thenickdude commentedon Jun 7, 2019
Yes, it's in the AMI but it's not in the actual Lambda runtime environment. Run this on Lambda Node 10.x:
This is the result:
The required libraries from my last post are about 100MB.
jwerre commentedon Jun 27, 2019
@thenickdude Thanks for your insight on this. I created a Lambda Layer which will address this issue.