由于 psycopg2,无法使用无服务器框架进行部署。 Docker psycopg2 安装不工作
Deploying with serverless framework not working because of psycopg2. Docker installation of psycopg2 not working
所以这是我的 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 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@ -> ../Cellar/postgresql/11.5_1/bin/pg_config
我还能做什么?简而言之,需要在 docker 中构建 psycopg2,以便创建的二进制文件适用于 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
。如果返回速度超快,您可能需要清除缓存版本。
提示:如果你让它编译,然后在调用该函数时出现 30 秒超时,你可能需要更改安全组 and/or 子网以确保它可以访问你的 Redshift 集群。
希望对您有所帮助!
我尝试了@vallard 的解决方案,但在 AWS Lambda 中出现错误:
ImportModuleError: Unable to import module 'handler.py' libpq.so.5: cannot open shared object file: No such file or directory
通过将 psycopg2-binary
添加到 serverless-python-requirements 插件使用的处理程序 requirements.txt 中,它终于开始工作了。
所以这是我的 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 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@ -> ../Cellar/postgresql/11.5_1/bin/pg_config
我还能做什么?简而言之,需要在 docker 中构建 psycopg2,以便创建的二进制文件适用于 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
。如果返回速度超快,您可能需要清除缓存版本。
提示:如果你让它编译,然后在调用该函数时出现 30 秒超时,你可能需要更改安全组 and/or 子网以确保它可以访问你的 Redshift 集群。
希望对您有所帮助!
我尝试了@vallard 的解决方案,但在 AWS Lambda 中出现错误:
ImportModuleError: Unable to import module 'handler.py' libpq.so.5: cannot open shared object file: No such file or directory
通过将 psycopg2-binary
添加到 serverless-python-requirements 插件使用的处理程序 requirements.txt 中,它终于开始工作了。