AWS Python 尝试访问 MySQL 时 Lambda 超时
AWS Python Lambda timeout when trying to access MySQL
我正在尝试访问我们的服务器 MySQL 数据库(不是 AWS RDS):
def lambda_handler(event, context):
try:
conn = pymysql.connect("mySite.com", user="admin", passwd="pass", connect_timeout=5)
我收到 return 错误:
Response:
{
"errorMessage": "xxxxxxxxxxxxxxxxxxxxxxxxx Task timed out after 3.00 seconds"
}
我已经使用 AWSLambdaVPCAccessExecutionRole
和 WSLambdaRole
设置了我的角色,并设置了带有子网和安全组的默认 VPC 网络:
Security group ID: sg-xxxxxxxx
Ports: All
Destination: 0.0.0.0/0
有什么我忘了的吗?我用 RDS 对此进行了测试,我能够连接到数据库,就在我尝试外部数据库时它正在超时。
如果数据库不在 VPC 中,则 Lambda 函数将无法访问它。当您 运行 VPC 内的 Lambda 函数时,不会为 Lambda 函数分配 public IP 地址。要使其能够访问 VPC 外部的资源,Lambda 函数必须位于私有子网中,并具有到 NAT 网关的路由。
或者,如果它不需要访问VPC内部的资源,那么只要将它从VPC中移除,它就可以上网了。
如果数据库在 VPC 运行ning EC2 服务器上,那么您可能需要适当地打开分配给 EC2 服务器的安全组。
我正在尝试访问我们的服务器 MySQL 数据库(不是 AWS RDS):
def lambda_handler(event, context):
try:
conn = pymysql.connect("mySite.com", user="admin", passwd="pass", connect_timeout=5)
我收到 return 错误:
Response:
{
"errorMessage": "xxxxxxxxxxxxxxxxxxxxxxxxx Task timed out after 3.00 seconds"
}
我已经使用 AWSLambdaVPCAccessExecutionRole
和 WSLambdaRole
设置了我的角色,并设置了带有子网和安全组的默认 VPC 网络:
Security group ID: sg-xxxxxxxx
Ports: All
Destination: 0.0.0.0/0
有什么我忘了的吗?我用 RDS 对此进行了测试,我能够连接到数据库,就在我尝试外部数据库时它正在超时。
如果数据库不在 VPC 中,则 Lambda 函数将无法访问它。当您 运行 VPC 内的 Lambda 函数时,不会为 Lambda 函数分配 public IP 地址。要使其能够访问 VPC 外部的资源,Lambda 函数必须位于私有子网中,并具有到 NAT 网关的路由。
或者,如果它不需要访问VPC内部的资源,那么只要将它从VPC中移除,它就可以上网了。
如果数据库在 VPC 运行ning EC2 服务器上,那么您可能需要适当地打开分配给 EC2 服务器的安全组。