I need to use my own version of boto3 inside a Lambda (Python 3.7). The version included in the Lambda default Python 3.7 env (1.9.42) does not support the use of Textract for one reason or another.
To do this, I did the following based on a guide:
- Create custom package using the following commands:
pip freeze > requirements.txt
which would yield this file:
# requirements.txt
boto3==1.9.138
botocore==1.12.138
docutils==0.14
jmespath==0.9.4
python-dateutil==2.8.0
s3transfer==0.2.0
six==1.12.0
urllib3==1.24.2
mkdir build
pip3 install -r requirements.txt -t build/
cd build
zip -r ../boto3_layer.zip .
Then I used the GUI to upload a new Lambda Layer (called boto3Layer). Then I added the layer to my Lambda Function successfully.
Problem is, I can't figure out how to import said layer into my code using the inline code editor.
I have the following code:
...
from boto3_layer as boto3
...
log.info(boto3)
I get the error "errorMessage": "Unable to import module 'lambda_function': No module named 'boto3_layer'"
I also tried importing just boto3
, but confirmed that it was the wrong version (it was the version used by Lambda), so my importing did not override it.
I'd like to simply know how to import my custom layer into my code! Thanks
edit: trying the suggestion:
For other users trying to accomplish the same task:
virtualenv python --python=python3.7
source python/bin/activate
and thenpip3 install boto3
zip -r boto3_layer.zip python/lib/
- Create new Lambda Layer with
boto3_layer.zip
and add layer to Lambda Function - Tried to run the save above code
Fails with
"errorMessage": "Unable to import module 'lambda_function': No module named 'boto3_layer'",
This ended up working by importing boto3
instead of my custom name.
import boto3
def lambda_handler(event, context):
textract = boto3.client('textract')