AWS Lambda 函数 Python 脚本任务超时错误
我尝试使用 Lambda 函数连接到 RDS postgresql 数据库.Lambda 函数返回超时错误.这是源代码.
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:
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:
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
这是执行结果:
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.
您的代码连接和检索数据可能只需要 3 秒以上的时间,因此您的 Lambda 函数会超时.您的默认设置为 3 秒,如您在 CloudWatch Logs 中所见):
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):
任务在 3.00 秒后超时
Task timed out after 3.00 seconds
增加 Lambda 的超时和配置的 RAM 大小(这将按比例为 Lambda 函数提供更多 CPU 并使其运行更快)并重试.
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.
如果这也失败了,那么问题可能是您的 Lambda 函数没有到 RDS 数据库的网络路由.没有路由会导致数据库连接尝试挂起和 Lambda 函数超时.
If that also fails, then the problem may be that your Lambda function has no network route to the RDS database. Having no route can cause the DB connection attempt to hang and the Lambda function to time out.
从您在下方评论中提供的信息来看,RDS 数据库似乎实际上在不同的 AWS 账户中运行,但可以公开访问.这表明您的 Lambda 函数可能配置不正确.如果您不需要 Lambda 函数在 VPC 中运行,则不要将其配置为在 VPC 中运行 - 请参阅 Lambda 函数 VPC 配置选项 并删除子网分配.
From the information you've provided in comments below, it looks like the RDS database is actually running in a different AWS account but is accessible publicly. This suggests that your Lambda function may be configured incorrectly. If you don't need the Lambda function to run in a VPC then don't configure it to run in VPC - see the Lambda function VPC configuration options and remove the subnet assignment(s).