Weasyprint: Deployment Package/Layer for AWS Lambda

Created on 7 Aug 2019  路  10Comments  路  Source: Kozea/WeasyPrint

The intent of this issue is to bring a community effort to get a documented way to get WeasyPrint working on an AWS Lambda environment.

There has been other issues and solutions related to this before but none were conclusive or adaptable

Key points :

  1. Users should be able to build the package themselves, choosing a weasyprint/python version combination (Only 3.6 and 3.7 are supported by Lambda)
  2. The main tool should be to use a docker with the lambci/lambda image, it mimicks aws lambda very closely, then install the packages and extract them
  3. Another issue is the file system in lambda, you only have access to the /tmp/ folder so libraries that need to be in a specific location need to be rethinked or find out if they're looking for a environment variable we can edit

Hopefully we can finally get this issue sorted, generating a PDF in AWS Lambda is a common scenario and it allow for a very cheap and fast hosting/deployment structure.

Also, once sorted out we can share with the community the ARN of a WeasyPrint Lambda Layer that anyone can use (sharing a lambda layer is free and incurs no additional charges)

documentation

Most helpful comment

I created a repository that creates the zip files for a lambda layer. (Used the same approach as @nelsonsar using lambci)

https://github.com/seanfdnn/weasyprint_lambda/

All 10 comments

I'll share what I have so far :

I'm trying to get a base idea using serverless-framework with serverless-python-requirements plugin, it automatically sets up a docker resembling aws lambda, then installs and packages all python packages from a requirements.txt

It also allows for extra packages to be included, for example I tried the following configuration setup, since installing just weasyprint makes it output cannot load library 'pango-1.0' :

Dockerfile

FROM lambci/lambda:build-python3.7

# Install your dependencies
RUN yum -y install pango

serverless.yml (just the plugin config part)

custom:
  pythonRequirements:
    dockerFile: Dockerfile
    dockerizePip: true
    layer: true
    dockerExtraFiles:
      - /usr/lib64/libpango-1.0.so.0
      - /usr/lib64/libpango-1.0.so.0.2800.1

plugins:
  - serverless-python-requirements

This in the end packages weasyprint along with libpango-1.0.so0 and libpango-1.0.so.0.2800.1 in the same folder.

Although this is a nice start, the same error still persists, I'm not sure where to go from here.

  1. Is a different version of pango needed?
  2. Are these the wrong files?
  3. Are they in the wrong location?

The intent of this issue is to bring a community effort to get a documented way to get WeasyPrint working on an AWS Lambda environment.

:heart:

This in the end packages weasyprint along with libpango-1.0.so0 and libpango-1.0.so.0.2800.1 in the same folder.

Reading #401 and talking to the people involved in this issue could be useful.

More info! It seems that when using layers, lambda looks for so libs in the /lib/ folder of the layer, so it won't work if it is in the same folder as weasyprint (is need to be in /python/ in a layer)

Docs for this here : https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path

I'll see if this is enough and hopefully try to drop the serverless-framework as requirement.

Also it only works with python3.6 lambda runtime, with python3.7 it doesn't find pango

I was able to deploy weasyprint using python3.6 runtime and layers. I used these two files here to achieve it: https://drive.google.com/drive/folders/1xEonJiIEGYuwdRPO4mayZbHAgQQq_epj?usp=sharing

I was able to deploy weasyprint using python3.6 runtime and layers. I used these two files here to achieve it

Thanks for sharing!

Could anybody write a short documentation about that? :heart:

Could anybody write a short documentation about that?

Oh, it's basically what's done in #952.

Thanks so much for this info, this is exactly what I needed. It would be helpful if @nelsonsar or someone who's generate these zipfiles would post the script or process used to re-generate them, so they can be iterated over?

@robbymeals I'm working on a repository to create the zip files using lambci. I'm struggling with time though, sorry for the delay 馃槄

I created a repository that creates the zip files for a lambda layer. (Used the same approach as @nelsonsar using lambci)

https://github.com/seanfdnn/weasyprint_lambda/

I created a repository that creates the zip files for a lambda layer. (Used the same approach as @nelsonsar using lambci)

https://github.com/seanfdnn/weasyprint_lambda/

I'm executing this command without error:

docker build -t weasyprint .

But I'm not getting the zip file in the weasyprint_lambda folder.

Please help

Was this page helpful?
0 / 5 - 0 ratings

Related issues

knyttl picture knyttl  路  4Comments

thejasechen picture thejasechen  路  3Comments

elcolie picture elcolie  路  4Comments

whitelynx picture whitelynx  路  5Comments

amarnav picture amarnav  路  5Comments