AWS Lambda import module error in python

后端 未结 21 1487
醉话见心
醉话见心 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:21

    If you are uploading a zip file. Make sure that you are zipping the contents of the directory and not the directory itself.

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

    In lambda_handler the format must be lambda_filename.lambda_functionName. Supposing you want to run the lambda_handler function and it's in lambda_fuction.py, then your handler format is lambda_function.lambda_handler.

    Another reason for getting this error is module dependencies.

    Your lambda_fuction.py must be in the root directory of the zip file.

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

    My problem was that the .py file and dependencies were not in the zip's "root" directory. e.g the path of libraries and lambda function .py must be:

    <lambda_function_name>.py
    <name of library>/foo/bar/
    

    not

    /foo/bar/<name of library>/foo2/bar2

    For example:

    drwxr-xr-x  3.0 unx        0 bx stor 20-Apr-17 19:43 boto3/ec2/__pycache__/
    -rw-r--r--  3.0 unx      192 bx defX 20-Apr-17 19:43 boto3/ec2/__pycache__/__init__.cpython-37.pyc
    -rw-r--r--  3.0 unx      758 bx defX 20-Apr-17 19:43 boto3/ec2/__pycache__/deletetags.cpython-37.pyc
    -rw-r--r--  3.0 unx      965 bx defX 20-Apr-17 19:43 boto3/ec2/__pycache__/createtags.cpython-37.pyc
    -rw-r--r--  3.0 unx     7781 tx defN 20-Apr-17 20:33 download-cs-sensors-to-s3.py
    
    0 讨论(0)
  • 2020-12-07 20:21

    No need to do that mess.

    use python-lambda

    https://github.com/nficano/python-lambda

    with single command pylambda deploy it will automatically deploy your function

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

    Please add below one after Import requests

    import boto3

    What I can see that is missing in your code.

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

    I found Nithin's answer very helpful. Here is a specific walk-through:

    Look-up these values:

    1. The name of the lambda_handler function in your python script. The name used in the AWS examples is "lambda_handler" looking like "def lambda_handler(event, context)". In this case, the value is "lambda_handler"
    2. In the Lambda dashboard, find the name of the Handler in the "Handler" text-box in the "Configuration" section in the lambda dashboard for the function (shown in Nithin's screenshot). My default name was "lambda_function.lambda_handler".
    3. The name of your python script. Let's say it's "cool.py"

    With these values, you would need to rename the handler (shown in the screenshot) to "cool.lambda_handler". This is one way to get rid of the "Unable to import module 'lambda_function'" errorMessage. If you were to rename the handler in your python script to "sup" then you'd need to rename the handler in the lambda dashboard to "cool.sup"

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