AWS Lambda中的Python请求超时
我正在尝试从我的AWS Lambda发出http请求,但是超时.
I'm trying to make a http request from my AWS Lambda but it times out.
我的代码与此类似:
import requests
def lambda_handler(event, context):
print('Im making the request')
request.get('http://www.google.com')
print('I recieved the response')
但是当我对此进行测试时,我会超时.
But when I test this, I get a timeout.
输出为
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代码.我只是想知道为什么它在该请求上超时.
So I know the issue isn't it not finding the request package, and it's running my python code. I just figure out why it times out on that request.
I encounter same timeout problem, the reason is below.
将VPC配置添加到Lambda函数时,它只能访问该VPC中的资源.如果Lambda功能需要同时访问VPC资源和公共Internet,则VPC需要在VPC内部具有一个网络地址转换(NAT)实例.
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时可能会出现一些错误. 我建议您可以遵循博客以构建NAT.
Maybe there are some error when you setting your VPC. I advice you can follow this blog to build NAT.