I was tried to connect into RDS postgresql database using Lambda function. Lambda function returning timed out error. Here is the source code.
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 users"
# 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 = "db_table"
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: " + query)
cursor = conn.cursor()
cursor.execute(query)
raw = cursor.fetchall()
for line in raw:
result.append(line)
return result
here is the execution results:
Test Event Name
triggers3event
Response
{
"errorMessage": "2021-10-19T22:55:12.543Z 207db48f-eb82-4bbd-afd5-1836b63874cf Task timed out after 3.00 seconds"
}
Function Logs
START RequestId: 207db48f-eb82-4bbd-afd5-1836b63874cf Version: $LATEST
END RequestId: 207db48f-eb82-4bbd-afd5-1836b63874cf
REPORT RequestId: 207db48f-eb82-4bbd-afd5-1836b63874cf Duration: 3003.71 ms Billed Duration: 3000 ms Memory Size: 128 MB Max Memory Used: 12 MB
2021-10-19T22:55:12.543Z 207db48f-eb82-4bbd-afd5-1836b63874cf Task timed out after 3.00 seconds
Request ID
207db48f-eb82-4bbd-afd5-1836b63874cf
Pls help me to fix following issue. I am not sure what was wrong from above codes.