I am going to connect RDS postgresql Database using Lambda function (python script)
I attached screenhot.
The error logs here.
Unable to import module 'postgres_test': No module named 'psycopg2'
python version is 3.6
This issue causing due to not installed psycopg2 package. Then I don't know how can I install the package on lambda Pls guide me for it.
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