postgresql 无法导入模块“lambda_function”:没有名为“psycopg2._psycopg”的模块

nxagd54h  于 2023-06-22  发布在  PostgreSQL
关注(0)|答案(1)|浏览(195)

我尝试使用Amazon RDS Postgres数据库调用lambda函数。当我尝试运行lambda函数时,我得到了这个错误:

{
      "errorMessage": "Unable to import module 'lambda_function': No module named'psycopg2._psycopg'",
      "errorType": "Runtime.ImportModuleError",
      "requestId": "c1c7e814-9560-4d5e-a71d-fa39fc33132e",
      "stackTrace": []
    }

我已经添加了psycopy2-binary包与此.

5gfr0r5j

5gfr0r5j1#

在底部的终端中逐行键入以下代码。pip install pandas命令可以替换为您选择的软件包。您也可以安装多个软件包 *。

mkdir folder
cd folder
virtualenv v-env
source ./v-env/bin/activate
pip3 install psycopg2
deactivate

然后逐行输入以下代码来创建图层

mkdir python
cd python
cp -r ../v-env/lib64/python3.7/site-packages/* .
cd ..
zip -r psycopg2_layer.zip python
aws lambda publish-layer-version --layer-name psycopg2 --zip-file fileb://psycopg2_layer.zip --compatible-runtimes python3.7

请参考下面的postgres层链接:-https://towardsdatascience.com/python-packages-in-aws-lambda-made-easy-8fbc78520e30

转到AWS Lambda函数->创建层

import psycopg2

db_endpoint = 'RDS-endpoint'
db_port = 5432
db_username = 'RDS-username'
db_password = 'RDS-password'
db_name = 'RDS-DB-name'

def lambda_handler(event, context):
  connection = psycopg2.connect(user=db_username, password=db_password, host=db_endpoint, database=db_name)
  cursor = connection.cursor()
  query = "SELECT version() AS version"
  cursor.execute(query)
  results = cursor.fetchone()
  print("results-----",results)
  cursor.close()
  connection.commit()
  return results

相关问题