use*_*688 8 python amazon-web-services aws-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')
Run Code Online (Sandbox Code Playgroud)
但是当我测试这个时,我会超时.
输出是
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
Run Code Online (Sandbox Code Playgroud)
所以我知道问题不是找不到请求包,而是运行我的python代码.我只想弄清楚为什么它会超时.
Lambda中的超时默认值为3秒= 3000微秒。转到高级设置,然后添加5分钟。如果超时恰好在3秒内发生,则这可能是唯一的问题。所有其他错误将花费更多或更少的代价。
除非您将NAT网关添加到VPC,否则具有VPC访问权限的Lambda函数将无法访问Internet。您应该阅读Lambda VPC支持公告中的“须知”部分。
如果您为Lambda功能启用了VPC支持,但是您的VPC中没有NAT网关,则您的请求正在尝试访问Internet超时。
小智 5
您可以通过执行以下操作来增加请求的超时时间:
response = requests.get(url, timeout=60)
Run Code Online (Sandbox Code Playgroud)
此外,您还需要增加 lambda 函数的超时时间。去做这个:
另外,我相信“request.get”应该是“requests.get”。
| 归档时间: |
|
| 查看次数: |
13747 次 |
| 最近记录: |