我们可以使用定时器和Lambda函数,来手动关闭AWS RDS Proxy未关闭的数据库连接。以下是解决方法的代码示例:
import boto3
def lambda_handler(event, context):
proxy_endpoint = 'your-proxy-endpoint-here'
rds_client = boto3.client('rds')
# 获取所有已登录的连接
db_connections = rds_client.describe_db_connections(
DBProxyEndpoint=proxy_endpoint,
MaxRecords=100
)
# 使用会话关闭连接
for conn in db_connections['DBConnections']:
conn_id = conn['DBConnectionIdentifier']
print(f'Closing DB connection {conn_id}')
rds_client.close_db_connection(
DBConnectionIdentifier=conn_id,
DBProxyEndpoint=proxy_endpoint
)
return 'Done closing DB connections'
cron(0/5 * * * ? *)
现在,Lambda函数将每5分钟运行一次,关闭AWS RDS代理未能关闭的数据库连接。