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.
CodePudding user response:
It might simply take your code more than 3 seconds to connect and retrieve data, so your Lambda function times out. You have the default setting of 3 seconds, as you can see in the CloudWatch Logs):
Task timed out after 3.00 seconds
Increase the timeout and the configured RAM size for the Lambda (which will give the Lambda function proportionally more CPU and make it run faster) and retry.
If that also fails, then explain your VPC setup (for the Lambda function and the RDS database).
CodePudding user response:
Usually the reason Lambda function times out is due to configuration of Security Groups.
In order to rectify, please follow these suggestions:
Check whether the Lambda and corresponding db service is in same VPC
Check whether the Security Group is allowing the connection
If you are connecting the db over internet, check the connection. (If you are in VPC, see you are routing via NAT and IG)
Again, reiterating one more time, that many times, timed out issue is to due to network allow/deny, so in case of Lambda, a Security Group issue.