完整截圖
運行時設定
我將使用我附加的螢屏截圖的 Lambda 函式(python 腳本)連接 RDS postgresql 資料庫。錯誤記錄在這里。
Unable to import module 'postgres_test': No module named 'psycopg2'
python版本是3.6
由于未安裝 psycopg2 軟體包而導致的此問題。然后我不知道如何在 lambda 上安裝該軟體包,請指導我。
postgres_test.py:
`
import sys
import logging
import psycopg2
from db_util import make_conn, fetch_data
def lambda_handler(event, context):
query_cmd = "select count(*) from tablename"
# print query_cmd
# get a connection, if a connect cannot be made an exception will be raised here
conn = make_conn()
result = fetch_data(conn, query_cmd)
conn.close()
return result
db_util.py:
`
?import psycopg2
?db_host = "db_host"
?db_port = 5432
?db_name = "db_name "
?db_user = "db_user "
?db_pass = "db_pass "
?db_table = "users"
?def make_conn():
?conn = None
?try:
?conn = psycopg2.connect("dbname='%s' user='%s' host='%s'
?password='%s'" % (db_name, db_user, db_host, db_pass))
?except:
?print "I am unable to connect to the database"
?return conn
?def fetch_data(conn, query):
?result = []
?print "Now executing: %s" % (query)
?cursor = conn.cursor()
?cursor.execute(query)
?raw = cursor.fetchall()
?for line in raw:
?result.append(line)
?return result
uj5u.com熱心網友回復:
要在 lambda 中使用不同的庫,您必須在當前專案中安裝該庫并將其作為 zip 檔案上傳到 lambda。
特定于 psycopg2 使用此 repo https://github.com/jkehler/awslambda-psycopg2
并安裝一些其他庫使用以下命令
例如requests庫
pip install requests -t .
您的專案將如下所示
.
├── lambda_function.py
├── psycopg2
├── <library2>
要使用 zip 檔案方法將專案上傳到 lambda,您可以使用以下鏈接
https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-package.html https://alexharv074.github.io/2018/08/18/creating-a-zip-file-for-an -aws-lambda-python-function.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/326725.html
標籤:Python 蟒蛇-3.x PostgreSQL 拉姆达 aws-lambda
