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:
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:
virtualenv python --python=python3.7
source python/bin/activate
and then pip3 install boto3
zip -r boto3_layer.zip python/lib/
boto3_layer.zip
and add layer to Lambda Function"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')
All AWS services and arguments are now available to your Lambda function. Tip: Use print(boto3. __version__) and print(botocore. __version__) in your function code to confirm the version of Boto 3 and Botocore.
Placing Newer Versions of boto3 and botocore Open a shell prompt into the directory containing the lambda handler, in this case, the lambda handler is within the file “lambda_function.py” within the innermost directory “NewBotoVersion” (the name of the function for this example).
Lambda provides runtimes for Python that run your code to process events. Your code runs in an environment that includes the SDK for Python (Boto3), with credentials from an AWS Identity and Access Management (IAM) role that you manage. Lambda supports the following Python runtimes.
AWS are deprecating Node. js 12 for AWS Lambda in two stages: Starting November 14, 2022, Lambda will no longer apply security patches and other updates to the Node. js 12 runtime used by Lambda functions, and functions using Node.
A directory will not be a module/package. The approach you're using will never work.
Instead of all this, create a virtual environment using the desired Python version - possibly within docker, wherein you install the packages. Then you'll have to zip the lib
folder from the virtual environment so that when unzipped the layer will produce the directory structure similar to
python/
lib/
pythonx.y/
site-packages/
...
The top-level directory must be named python
for this to work.
If you do this correctly, then you should be able to import your version of boto3
normally.
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