Brett
Brett

Reputation: 6020

AWS Lambda unable to import module from layer

I am trying to create a Lambda function with a dependency to load from a layer. I thought I had this working when I deployed it a while ago, but I keep getting this error now, and I can't figure out what I am doing wrong here.

I am trying to create a layer with pyopenssl for python 3.x. My understanding is that the dependencies should be installed into a "python" top level directory before being zipped for the layer. Here is how the layer is created:

requirements.txt:

$ echo "pyopenssl" > requirements.txt
pip install -t python -r requirements.txt
python -c "import shutil;shutil.make_archive('layer-openssl','zip','python')"

The python command is the equivalent of creating a zip file from the python directory. e.g. if I create a temp directory and unzip in there, the top level directory is python with OpenSSL and all the transitive dependencies in it (cffi, cryptography, etc).

Python version is 3.8, and pip is 20.0 from python 3.8 too.

I then create the layer (e.g. for testing at the moment, I am doing this manually in the web console) - have marked the layer as compatible with 3.6, 3.7 and 3.8 versions.

Then I create a Python 3.8 function, and add an import:

from OpenSSL.crypto import load_certificate

def lambda_handler(event, context):
  return true

And this gives me an error: Unable to import module 'lambda_function': No module named OpenSSL.crypto

I have tried publishing a 2nd version of the layer marked as compatible with Python 2.7, and then created a 2.7 runtime function with the same import, but it fails too.

Is there anything obvious that I am doing wrong that I am missing? Should adding OpenSSL to python be sufficient - i am pretty sure i had this working with python 3.8 a short while ago, and am using the same deployment code on this.

Upvotes: 2

Views: 2242

Answers (1)

Brett
Brett

Reputation: 6020

The loading works if I add a variable of PYTHONPATH set to /opt to the lambda. It looks like "/opt/python" is not searched by default.

Not sure how it worked previously

Upvotes: 4

Related Questions