Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cairosvg not working when deployed to AWS Lambda python 3.8 #384

Open
Dipankar000 opened this issue Mar 31, 2023 · 5 comments
Open

Cairosvg not working when deployed to AWS Lambda python 3.8 #384

Dipankar000 opened this issue Mar 31, 2023 · 5 comments

Comments

@Dipankar000
Copy link

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.

@grewn0uille
Copy link
Member

Hello,

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

@Dipankar000
Copy link
Author

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

@grewn0uille
Copy link
Member

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?

@goodpaul
Copy link

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!

@Dipankar000
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants