AWS Lambda import module error in python

后端 未结 21 1485
醉话见心
醉话见心 2020-12-07 19:56

I am creating a AWS Lambda python deployment package. I am using one external dependency requests . I installed the external dependency using the AWS documentation http://do

相关标签:
21条回答
  • 2020-12-07 20:06

    I found this hard way after trying all of the solutions above. If you're using sub-directories in the zip file, ensure you include the __init__.py file in each of the sub-directories and that worked for me.

    0 讨论(0)
  • 2020-12-07 20:06

    I had the error too. Turn out that my zip file include the code parent folder. When I unzip and inspect the zip file, the lambda_function file is under parent folder ./lambda.

    Use the zip command, fix the error:

    zip -r ../lambda.zip ./*
    
    0 讨论(0)
  • 2020-12-07 20:06

    The issue here that the Python version used to build your Lambda function dependencies (on your own machine) is different than the selected Python version for your Lambda function. This case is common especially if the Numpy library part of your dependencies.

    Example: Your machine's python version: 3.6 ---> Lambda python version 3.6

    0 讨论(0)
  • 2020-12-07 20:06

    Make sure that you are zipping all dependencies in a folder structure as python/[Your All Dependencies] to make it work as per this documentation.

    https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path

    0 讨论(0)
  • 2020-12-07 20:07

    Here's a quick step through.

    Assume you have a folder called deploy, with your lambda file inside call lambda_function.py. Let's assume this file looks something like this. (p1 and p2 represent third-party packages.)

    import p1
    import p2
    
    def lambda_handler(event, context):
        # more code here
    
        return {
            "status": 200,
            "body" : "Hello from Lambda!",
        }
    

    For every third-party dependency, you need to pip install <third-party-package> --target . from within the deploy folder.

    pip install p1 --target .
    pip install p2 --target .
    

    Once you've done this, here's what your structure should look like.

    deploy/
    ├── lambda_function.py
    ├── p1/
    │   ├── __init__.py
    │   ├── a.py
    │   ├── b.py
    │   └── c.py
    └── p2/
        ├── __init__.py
        ├── x.py
        ├── y.py
        └── z.py
    

    Finally, you need to zip all the contents within the deploy folder to a compressed file. On a Mac or Linux, the command would look like zip -r ../deploy.zip * from within the deploy folder. Note that the -r flag is for recursive subfolders.

    The structure of the file zip file should mirror the original folder.

    deploy.zip/
    ├── lambda_function.py
    ├── p1/
    │   ├── __init__.py
    │   ├── a.py
    │   ├── b.py
    │   └── c.py
    └── p2/
        ├── __init__.py
        ├── x.py
        ├── y.py
        └── z.py
    

    Upload the zip file and specify the <file_name>.<function_name> for Lambda to enter into your process, such as lambda_function.lambda_handler for the example above.

    0 讨论(0)
  • 2020-12-07 20:08

    Actually go to the main folder (deployment package)that you want to zip,

    Inside that folder select all files and then create the zip and upload that zip

    0 讨论(0)
提交回复
热议问题