简体   繁体   中英

How to downgrade the boto3 version in an AWS Lambda Function

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 :

  1. 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:

  1. virtualenv python --python=python3.7
  2. source python/bin/activate and then pip3 install boto3
  3. zip -r boto3_layer.zip python/lib/
  4. Create new Lambda Layer with boto3_layer.zip and add layer to Lambda Function
  5. 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')

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.

The files in the lambda will be unzipped to a directory named /opt and this will be included in the PYTHONPATH . This means that you do need an extra boto3_layer in your zip structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM