I have a function with a layer that has an import inside of it.

When ran, I get the error "No module named xhtml2pdf"

I have 2 layers that I could import, one for the PDF script and one for the xhtml2pdf module, I don't really want to import both

My import of the function in the layer is:

from PDF import convert_html_to_pdf

Inside the layer I have the import

from xhtml2pdf import pisa

Do I need to add the package into my main function using a layer or can I "nest" layers inside of each other?

Or, is there a way to install the package inside the same layer as my PDF.py?


Generally speaking, you should try and have it so the lambda layer is specific to the lambda function, meaning that the lambda function and the layer have a one to one relationship. As the lambda function changes, so does that layer to accommodate more or less libraries required.

While lambda layers can indeed be reused across different lambda functions, a one to many relationship for the layer makes it trickier to effectively manage the lambda functions; it's also unlikely that the layer can be re-used as is by different lambda functions, which then means you need more layers. You can see why this could become painful.

This then implies that the best solution is to install all dependencies your function needs into a single layer. You can find full instructions on how to do this here.