Cannot use ghostscript in AWS Lambda

Viewed 2499

I'm trying to use ghostscript to convert pdfs to tiffs in AWS Lambda. Everything works locally and I get good, fast conversions. I was told Lambda has an (old) version of ghostscript built in that is ready to use but I can't seem to be able to call on it.

I tried using the os.system command to call ghostscript directly and also tried using the pip installed version of ghostscript but neither worked.

os.system("gs -q -dBATCH -dNOPAUSE "
                "-sDEVICE=tiffg4 -r600 "
                "-sOutputFile=" + upload_path + " " + download_path)

I'm getting this following error :

sh: gs: command not found

and I don't understand why or how to get around it.

2 Answers

I followed the instructions in this doc by starting up an ec2 instance with the correct AMI (first link here for me), but that only helps you get the shared libraries out, which ended up being the following command when that part comes:

yumdownloader ghostscript.x86_64 avahi-libs.x86_64 cairo.x86_64 cups-libs.x86_64 ghostscript-fonts.noarch gnutls.x86_64 jasper-libs.x86_64 jbigkit-libs.x86_64 libXdamage.x86_64 libXfixes.x86_64 libXt.x86_64 libXxf86vm.x86_64 libdrm.x86_64 libglvnd.x86_64 libglvnd-glx.x86_64 libpciaccess.x86_64 libtiff.x86_64 libxshmfence.x86_64 mesa-dri-drivers.x86_64 mesa-filesystem.x86_64 mesa-libGL.x86_64 mesa-libglapi.x86_64 pixman.x86_64 urw-fonts.noarch

So I installed ghostscript 8.70 with yum, got the share & bin folders from /tmp/ghostscript-8.70-24.26.amzn1.x86_64/usr after installation.
The libraries you'll get by following the link above go in /lib and I put the /bin and /shared in /ghostscript in the lambda zip. So now the zip for my node.js function looks like:
src/
ghostscript/
lib/
...etc

I changed the script to use LAMBDA_TASK_ROOT to build the binary path for gs (which is now /var/task/ghostscript/bin/gs), but got an error saying it couldn't find gs_init.ps. In this version they used share/Resource/Init for these files, so I added this location as an environment variable on the lambda function (KEY: GS_LIB, VALUE: /var/task/ghostscript/share/ghostscript/8.70/Resource/Init) and all is working again.

Related