Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run binary like `pdflatex` on AWS Lambda?

Since AWS Lambda supports running binaries, I wanted to run pdflatex in AWS Lambda, but I stumbled upon a few problems. I have successfully ran other binaries, but pdflatex has many shared OS libraries dependencies and I couldn't figure out how to make it work.

The sample code looks like this:

'use strict';
let exec = require('child_process').exec;

exports.handler = (event, context, callback) => {
  const child = exec('LD_LIBRARY_PATH=bin/ ./pdflatex my-file.tex', (error) => {
    callback(error, 'Process complete!');
  });
  child.stdout.on('data', console.log);
  child.stderr.on('data', console.error);
};

And the ZIP file looks like this:

index.js -------------- where the above code is
pdflatex -------------- binary from my OS
my-file.tex ----------- a sample LaTeX document
bin/ ------------------ folder with shared OS libraries

The uploaded ZIP file contains all of the above.

I generated the pdflatex with cp $(which pdflatex) pdflatex.

  1. Is this the right way to upload the pdflatex it? Is there any problem with this method? Is there a better way?

After trying to run for the first time, AWS Lambda complained that many lib_____.so were missing, so I copied them from my machine to the bin/ folder inside of the ZIP. I used ldd $(which pdflatex) to look for pdflatex's dependencies.

However, as I imagined, they were incompatible with the Linux version that runs AWS Lambda, so I fired a CentOS and copied the lib______.so files to the bin/ folder, but this didn't work either.

  1. Is there a way to "dump" a self-contained version of pdflatex that contained all the required dependencies and would run with no need of shared OS libraries?

  2. Do I need to compile pdflatex inside an Amazon Linux instance? What if I compiled it in a 64 bit architecture and the code ran on a 32 bit architecture, wouldn't it fail?

like image 945
EuAndreh Avatar asked Mar 13 '23 07:03

EuAndreh


1 Answers

The best way to generate binaries for use in a Lambda is to either copy or compile them on the same version of Linux that Lambda uses. Amazon has a list of AMI images here: http://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html. Create an EC2 instance from one of those, install the packages, and copy the files into your zip as you've done.

For #2, you can simplify the dependencies by doing a static compile, but I wouldn't try that unless you're familiar with building packages from source. You'll need to compile with -static at the link stage.

like image 77
ataylor Avatar answered Mar 14 '23 20:03

ataylor