Python 3.x 由于psycopg2,使用无服务器框架部署无法工作。psycopg2的Docker安装不工作
这是我的serverless.yml文件,它是相关的:Python 3.x 由于psycopg2,使用无服务器框架部署无法工作。psycopg2的Docker安装不工作,python-3.x,aws-lambda,psycopg2,Python 3.x,Aws Lambda,Psycopg2,这是我的serverless.yml文件,它是相关的: plugins: - serverless-python-requirements # registers the plugin with Serverless # hooking into the Framework on a deploy command. Before your package is zipped, it uses Docker to install the # packages listed in your r
plugins:
- serverless-python-requirements
# registers the plugin with Serverless
# hooking into the Framework on a deploy command. Before your package is zipped, it uses Docker to install the
# packages listed in your requirements.txt file and save them to a .requirements/ directory. It then symlinks the
# contents of .requirements/ into your top-level directory so that Python imports work as expected.
custom:
pythonRequirements:
dockerizePip: non-linux
zip: true
slim: true
在我的requirements.txt
文件中,我有这样一个:psycopg2==2.8.3
当我运行sls deploy
时,我看到:
Error: pg_config executable not found.
pg_config is required to build psycopg2 from source. Please add the directory
containing pg_config to the $PATH or specify the full executable path with the
option: python setup.py build_ext --pg-config /path/to/pg_config build
我的pg_-config
脚本在/env/local/bin
中作为:pg_-config->../ceral/postgresql/11.5_1/bin/pg_-config
我还能做什么?简而言之,psycopg2需要在docker中构建,以便创建的二进制文件适合aws lambda。我无法使用
serverless python requirements
插件实现这一点。我还能做什么?我也有同样的问题,我看到的许多帖子对我没有帮助。例如,本文[1]在需要正确设置安全组和子网方面提供了很大的帮助,但是查看代码[2]我不知道它是如何安装的,因为它会出现相同的错误
我所做的是在顶级无服务器目录中创建了一个Dockerfile,然后注入所需的文件。Dockerfile如下所示:
FROM lambci/lambda:build-python3.7
RUN yum install -y postgresql-devel python-psycopg2 postgresql-libs
然后在serverless.yaml
文件中,我添加了以下内容:
custom:
pythonRequirements:
dockerizePip: non-linux
dockerFile: ./Dockerfile
从文档[3]中,这将获取Dockerfile(现在有pg_config
)并将requirements.txt
添加到其中。如果缓存版本很快恢复,您可能需要清除缓存版本
Protip:如果您让它编译,然后在调用函数时超时30秒,那么您可能需要更改安全组和/或子网,以确保它可以访问红移群集
希望有帮助
我尝试了@vallard的解决方案,但在AWS Lambda中出现了一个错误:
ImportModuleError: Unable to import module 'handler.py' libpq.so.5: cannot open shared object file: No such file or directory
通过将
psycopg2binary
添加到处理程序的requirements.txt(由无服务器python需求插件使用)中,最终实现了该功能。使用psycopg2binary
。有关更多信息: