我有一个在 API Gateway 和 Lambda 函数上运行的 Flask API,其中我的 Lambda 函数被配置为在我的 VPC 中运行。
我的 Lambda 函数的正常持续时间应该是大约 3 秒,但有时它会飙升至 130 秒或更长,这会导致我的 API 网关返回 504。
Lambda 函数使用requests库发出 GET 请求:
url = base_url + endpoint
req = requests.get(url, headers=headers)
response = json.loads(req.content.decode('utf-8'))
Run Code Online (Sandbox Code Playgroud)
CloudWatch 在超时请求上显示以下错误:
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='host', port=port): Max retries exceeded with url: /foo/bar (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at foo>: Failed to establish a new connection: [Errno 110] Connection timed out'))
Run Code Online (Sandbox Code Playgroud)
我读过的大部分帖子都提到了在私有子网中运行的错误配置的 Lambda 函数,但我知道这不是我的问题,因为我的函数可以访问互联网。
我的另一个理论是会话正在函数的底层容器上重用,这导致超时。
提前感谢您的帮助!