问题
I have a python script named foo.py
. It has a lambda handler function defined like this:
def handler(event, context):
for record in event['Records']:
bucket = record['s3']['bucket']['name']
key = record['s3']['object']['key']
download_path = '/tmp/{}.gz'.format(key)
csv_path = '/tmp/{}.csv'.format(key)
... proceed to proprietary stuff
This is in a zip file like so:
-foo.zip
-foo.py
-dependencies
I have uploaded this zip file to AWS Lambda and configured an AWS Lambda Function to run foo.handler
. However, every time I test it, I get "errorMessage": "Unable to import module 'foo'"
.
Any ideas what might be going on here?
stat --format '%a' foo.py
shows 664
回答1:
So, I was importing psycopg2
in my lambda function, which requires libpq.so
, which installs with Postgres. Postgres isn't installed in the lambda environment, so importing psycopg2
failed, which meant that, by extension, Amazon's import of my lambda function also failed. Not a very helpful error message, though.
Thankfully, somebody's built a version of psycopg2
that works with AWS lambda: https://github.com/jkehler/awslambda-psycopg2
来源:https://stackoverflow.com/questions/42494289/unable-to-import-module-in-aws-lambda-python