使用 psycopg2 的 Lambda 函数 python 脚本
Lambda function python script using psycopg2
Full screenshot
Runtime settings
我将使用 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
要在 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
Full screenshot
Runtime settings
我将使用 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
要在 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