CairoSVG icon indicating copy to clipboard operation
CairoSVG copied to clipboard

Cairosvg not working when deployed to AWS Lambda python 3.8

Open Dipankar000 opened this issue 1 year ago • 5 comments

Cairosvg not working when deployed to AWS Lambda python 3.8 (Amazon linux 2). Working with python 3.7 (Amazon linux 1).

Python: OSError: cannot load library libcairo.so.2

cannot load library ‘libcairo.so’: libcairo.so: cannot open shared object file: No such file or directory cannot load library ‘libcairo.2.dylib’: libcairo.2.dylib: cannot open shared object file: No such file or directory cannot load library ‘libcairo-2.dll’: libcairo-2.dll: cannot open shared object file: No such file or directory

Using with reportlab.

Dipankar000 avatar Mar 31 '23 07:03 Dipankar000

Hello,

Did you install the cairo library on your Amazon Linux 2? You can install it with the package manager of the distribution.

grewn0uille avatar Apr 03 '23 09:04 grewn0uille

Yes, Amazon linux 2 is part of AWS lambda. I am packaging everything and deploying it using serverless framework.

Dipankar000 avatar Apr 03 '23 09:04 Dipankar000

Using the amazonlinux:2 docker image and the following steps, I can’t reproduce the issue:

  • amazon-linux-extras enable python3.8
  • yum install python38
  • yum install cairo
  • pip3.8 install cairosvg
  • python3.8 and import cairosvg

Can you reproduce your issue with this Docker image?

grewn0uille avatar Apr 03 '23 14:04 grewn0uille

It's working in the docker image because it's able to reference the dependencies from the Linux OS that comes from running yum install cairo. You need to copy those dependencies to your lambda function. Running ldd /usr/lib64/libcairo.so.2 told me the dependencies it had then I put together these commands to move those .so files out:

cp libpthread.so.* /io/requirements/
cp libpixman-1.so.* /io/requirements/
cp libfontconfig.so.* /io/requirements/
cp libfreetype.so.* /io/requirements/
cp libEGL.so.* /io/requirements/
cp libdl.so.* /io/requirements/
cp libpng15.so.* /io/requirements/
cp libxcb-shm.so.* /io/requirements/
cp libxcb.so.* /io/requirements/
cp libxcb-render.so.* /io/requirements/
cp libXrender.so.* /io/requirements/
cp libX11.so.* /io/requirements/
cp libXext.so.* /io/requirements/
cp libz.so.* /io/requirements/
cp libGL.so.* /io/requirements/
cp librt.so.* /io/requirements/
cp libm.so.* /io/requirements/
cp libc.so.* /io/requirements/
cp libexpat.so.* /io/requirements/
cp libuuid.so.* /io/requirements/
cp libbz2.so.* /io/requirements/
cp libGLdispatch.so.* /io/requirements/
cp libXau.so.* /io/requirements/
cp libGLX.so.* /io/requirements/

On the lambda side, you need to put these somewhere your code can see the shared libraries. I didn't have luck setting the LD_LIBRARY_PATH to a custom folder for some reason. However, I did have luck putting all .so.* files into a folder called "lib" and then compressing it and applying it via a lambda layer. This makes it accessible via /opt/lib, which is apparently a default place for lambda functions to house dependencies. After doing this, my script could finally use CairoSVG from a Python3.8 lambda function!

Good luck!

goodpaul avatar Apr 16 '23 21:04 goodpaul

Thanks for the reply @goodpaul. I will try with lambda layer.

Dipankar000 avatar Apr 17 '23 03:04 Dipankar000