Unable to import module in AWS Lambda (Python)

匿名 (未验证) 提交于 2019-12-03 09:05:37

问题:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!