AWS Lambda import module error in python

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

    There are just so many gotchas when creating deployment packages for AWS Lambda (for Python). I have spent hours and hours on debugging sessions until I found a formula that rarely fails.

    I have created a script that automates the entire process and therefore makes it less error prone. I have also wrote tutorial that explains how everything works. You may want to check it out:

    Hassle-Free Python Lambda Deployment [Tutorial + Script]

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

    Another source of this problem is the permissions on the file that is zipped. It MUST be at least world-wide readable. (min chmod 444)

    I ran the following on the python file before zipping it and it worked fine.

    chmod u=rwx,go=r
    
    0 讨论(0)
  • 2020-12-07 20:13

    @nithin, AWS released layers concept inside Lambda functions. You can create your layer and there you can upload as much as libraries and then you can connect the layer with the lambda functions. For more details: https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html

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

    A perspective from 2019:

    AWS Lambda now supports Python 3.7, which many people (including myself) choose to use as the runtime for inline lambdas.

    Then I had to import an external dependency and I followed AWS Docs as the OP referred to. (local install --> zip --> upload).

    I had the import module error and I realised my local PC had Python 2.7 as the default Python. When I invoked pip, it installed my dependency for Python 2.7.

    So I switched locally to the Python version that matches the selected runtime version in the lambda console and then re-installed the external dependencies. This solved the problem for me. E.g.:

    $ python3 -m pip install --target path/to/lambda_file <external_dependency_name>
    
    0 讨论(0)
  • 2020-12-07 20:14

    Sharing my solution for the same issue, just in case it helps anyone.

    Issue: I got error: "[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'StringIO'" while executing aws-big-data-blog code[1] provided in AWS article[2].

    Solution: Changed Runtime from Python 3.7 to Python 2.7

    [1] — https://github.com/bsnively/aws-big-data-blog/blob/master/aws-blog-vpcflowlogs-athena-quicksight/CloudwatchLogsToFirehose/lambdacode.py [2] — https://aws.amazon.com/blogs/big-data/analyzing-vpc-flow-logs-with-amazon-kinesis-firehose-amazon-athena-and-amazon-quicksight/

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

    I ran into the same issue, this was an exercise as part of a tutorial on lynda.com if I'm not wrong. The mistake I made was not selecting the runtime as Python 3.6 which is an option in the lamda function console.

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