I have a file with this as the contents.
def print_hello_world():
print ('Hello World')
It's zipped up in a folder with a __init__.py
file.
I add this as the layer and set the correct runtime of python3.6.
How do import it into my lambda function via lambda code?
Edit: After researching I found that lambda mounts the layers at /opt and if you add /opt to your syspath via sys.path.insert(0, '/opt') then you can import your layers.
You can see your layers at: print(os.listdir("/opt"))
There's gotta be a more elegant way to do this!
So I've recently ran into this issue, and I believe I found a cleaner way to import your layers.
First for the structure of the zip file which you upload:
__init__.py
filepython
import {filename}
So if your script in the python
folder was called something like custom_helper.py
, import it in your lambda with import custom_helper
.
I am not sure if this is the clean way to do it, but it seems simple enough to start.
Your zip file should have the following structure:
python/lib/python3.7/site-packages
That is, it needs a folder named Python, and within that a folder named lib, and within that a folder named python3.7, and within that a folder named site-packages. Anything inside that folder will be available for import.
(If you're using another version of Python, that version should be in the path instead of 3.7)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With