Python AWS Lambda 中的请求超时
Python request in AWS Lambda timing out
我正在尝试从我的 AWS Lambda 发出 http 请求,但它超时了。
我的代码看起来与此类似:
import requests
def lambda_handler(event, context):
print('Im making the request')
request.get('http://www.google.com')
print('I recieved the response')
但是当我测试这个时,我超时了。
输出为
Im making the request
END RequestId: id
REPORT RequestId: id Duration: 15003.25 ms Billed Duration: 15000 ms Memory Size: 128 MB Max Memory Used: 18 MB
2016-04-08T20:33:49.951Z id Task timed out after 15.00 seconds
所以我知道问题不是找不到请求包,而是 运行 我的 python 代码。我只是弄清楚为什么它会在该请求上超时。
具有 VPC 访问权限的 Lambda 函数将无法访问互联网,除非您将 NAT 网关添加到您的 VPC。您应该阅读 Lambda VPC support announcement 的 "Things to Know" 部分。
如果您为 Lambda 函数启用了 VPC 支持,但您的 VPC 中没有 NAT 网关,那么您的请求在尝试访问互联网时超时。
您可以通过执行以下操作来增加请求的超时期限:
response = requests.get(url, timeout=60)
另外,您需要增加 lambda 函数的超时时间。为此:
- 在 AWS 中打开您的 lambda 函数
- Select'Configuration',然后'Advanced Settings'
- 增加超时时间(最多 5 分钟)
- Select 'Save'
此外,我认为 'request.get' 应该是 'requests.get'。
我遇到了同样的情况,原因如下。
When you add VPC configuration to a Lambda function, it can only access resources in that VPC. If a Lambda function needs to access both VPC resources and the public Internet, the VPC needs to have a Network Address Translation (NAT) instance inside the VPC.
可能是您在设置VPC时出现了一些错误。
我建议你可以按照这个 blog 来构建 NAT。
Lambda 中超时的默认值为 3 秒 = 3000 微秒。
转到高级设置并添加 5 分钟。
如果超时恰好发生在 3 秒,这可能是唯一的问题。所有其他错误都会或多或少地发生。
默认情况下,AWS Lambda 有 3 秒的超时时间,因此如果您的代码 运行 超过 3 秒,它会自动超时给您这个错误。
您可以将 lambda 函数的超时时间增加到最多 5 分钟(300 秒 - AWS 将来可能会增加此限制),这应该可以解决问题。
此外,您的代码应反映 requests.get 而不是 request.get
我在Lambda函数中配置了VPC配置,还添加了NAT网关,将函数时间设置为5分钟。
问题 timed out after XX-- seconds
仍然存在。通过将 context.callbackWaitsForEmptyEventLoop 设置为 false 为我解决了这个问题。
我也有这个问题。如果您在 VPC 之外使用 运行 lambda,请确保您位于支持 SES 的区域中。如果区域不支持它,lambda 函数总是会失败。
我正在尝试从我的 AWS Lambda 发出 http 请求,但它超时了。
我的代码看起来与此类似:
import requests
def lambda_handler(event, context):
print('Im making the request')
request.get('http://www.google.com')
print('I recieved the response')
但是当我测试这个时,我超时了。
输出为
Im making the request
END RequestId: id
REPORT RequestId: id Duration: 15003.25 ms Billed Duration: 15000 ms Memory Size: 128 MB Max Memory Used: 18 MB
2016-04-08T20:33:49.951Z id Task timed out after 15.00 seconds
所以我知道问题不是找不到请求包,而是 运行 我的 python 代码。我只是弄清楚为什么它会在该请求上超时。
具有 VPC 访问权限的 Lambda 函数将无法访问互联网,除非您将 NAT 网关添加到您的 VPC。您应该阅读 Lambda VPC support announcement 的 "Things to Know" 部分。
如果您为 Lambda 函数启用了 VPC 支持,但您的 VPC 中没有 NAT 网关,那么您的请求在尝试访问互联网时超时。
您可以通过执行以下操作来增加请求的超时期限:
response = requests.get(url, timeout=60)
另外,您需要增加 lambda 函数的超时时间。为此:
- 在 AWS 中打开您的 lambda 函数
- Select'Configuration',然后'Advanced Settings'
- 增加超时时间(最多 5 分钟)
- Select 'Save'
此外,我认为 'request.get' 应该是 'requests.get'。
我遇到了同样的情况
When you add VPC configuration to a Lambda function, it can only access resources in that VPC. If a Lambda function needs to access both VPC resources and the public Internet, the VPC needs to have a Network Address Translation (NAT) instance inside the VPC.
可能是您在设置VPC时出现了一些错误。 我建议你可以按照这个 blog 来构建 NAT。
Lambda 中超时的默认值为 3 秒 = 3000 微秒。 转到高级设置并添加 5 分钟。 如果超时恰好发生在 3 秒,这可能是唯一的问题。所有其他错误都会或多或少地发生。
默认情况下,AWS Lambda 有 3 秒的超时时间,因此如果您的代码 运行 超过 3 秒,它会自动超时给您这个错误。
您可以将 lambda 函数的超时时间增加到最多 5 分钟(300 秒 - AWS 将来可能会增加此限制),这应该可以解决问题。
此外,您的代码应反映 requests.get 而不是 request.get
我在Lambda函数中配置了VPC配置,还添加了NAT网关,将函数时间设置为5分钟。
问题 timed out after XX-- seconds
仍然存在。通过将 context.callbackWaitsForEmptyEventLoop 设置为 false 为我解决了这个问题。
我也有这个问题。如果您在 VPC 之外使用 运行 lambda,请确保您位于支持 SES 的区域中。如果区域不支持它,lambda 函数总是会失败。