user8738575
user8738575

Reputation:

Importing PyNaCl into Lambda Function - No Module named "_cffi_backend"

I'm was able to add my PyNaCl library as a layer into Lambda (Python 3.8) but for some reason when I try and test the code I get the error

"errorMessage": "Unable to import module 'lambda_function': No module named '_cffi_backend'", "errorType": "Runtime.ImportModuleError"

Now when I use PyCharm locally and install the PyNaCl library into the venv, I have no execution errors. Does Lambda require the file hierarchy to be different? I zipped up the library as /lib/python3.8/site-packages with the only library included being PyNaCl

Upvotes: 0

Views: 6731

Answers (2)

yuklamwong2609
yuklamwong2609

Reputation: 31

There are 2 possible reasons to your problem:

  1. The lib folder structure is inaccurate. Try python/lib/python3.8/site-packages, then zip and upload again

  2. If (1) does not work, this is likely the library you used was compiled on a platform not compatible to Amazon Lambda. Amazon Lambda is based on Amazon Linux. A simple way is to create a docker image with the docker file provided by AWS(note your python version):

https://docs.aws.amazon.com/lambda/latest/dg/python-image.html

Then compile the PyNaCl libraries within the container, take out the compiled library files and do the upload again.

Upvotes: 3

alexis-donoghue
alexis-donoghue

Reputation: 3387

Lambda requires that layer .zip archive directory structure starts with python dir. Usually what works is this:

mkdir python
pip install pynacl -t python

This should create correct structure. Zip and upload as a layer.

Alternatively you can build docker image with dependencies and use it in lambda: https://docs.aws.amazon.com/lambda/latest/dg/images-create.html

Upvotes: 0

Related Questions