简体   繁体   English

使用 Python 在 Lambda 中获取请求任务超时

[英]get request Task timed out in Lambda using Python

I'm new to AWS and Python.我是 AWS 和 Python 的新手。 I'm trying to get the results of HTTP requests through Lambda.我正在尝试通过 Lambda 获取 HTTP 请求的结果。 I attached the layer which includes requests library.我附加了包含请求库的层。

Here is the problem:这是问题所在:

Runtime:Python 3.7运行时:Python 3.7

import requests
# import urllib.request *already tried
# from botocore.vendored import requests *already tried

def lambda_handler(event, context):
        params = (
        ('hoge', 'fuga'),
        ('piyo', 'pipiyo')
    )

    print('API start')
    response = requests.get('https://sample.com', params=params)
    if response is None:
        print('no response')
    print(response)

# get the first num of status code
    status = response.status_code
    print(status)
    status_head = status[1]
    output = response.json()
    print(output)

# get the result and error message from body
    json_data = json.load(output)
    result = json_data["result"]
    error = json_data["error"]
    print(result)
    print(error)
    return result

What I get is我得到的是

Response:
{
  "errorMessage": "2020-06-24T05:50:43.309Z 035e9470-c067-40bb-adc3-7c5b1f72f885 Task timed out after 60.06 seconds"
}

Request ID:
"035e9470-c067-40bb-adc3-7c5b1f72f885"

Function Logs:
START RequestId: 035e9470-c067-40bb-adc3-7c5b1f72f885 Version: $LATEST
API start
END RequestId: 035e9470-c067-40bb-adc3-7c5b1f72f885
REPORT RequestId: 035e9470-c067-40bb-adc3-7c5b1f72f885  Duration: 60058.03 ms   Billed Duration: 60000 ms   Memory Size: 128 MB Max Memory Used: 73 MB  Init Duration: 789.24 ms    
2020-06-24T05:50:43.309Z 035e9470-c067-40bb-adc3-7c5b1f72f885 Task timed out after 60.06 seconds

When I send this request from Talend API Tester, the response returns right.当我从 Talend API 测试仪发送此请求时,响应返回正确。

Do I have to change parameters of test events?我必须更改测试事件的参数吗? Does that matter?这有关系吗? Current content is当前内容是

{
  "key1": "value1",
  "key2": "value2"
}

What am I missing here?我在这里想念什么? Please tell me if I have to add more info.请告诉我是否需要添加更多信息。 Thanks in advance!提前致谢!

A very likely reason why your lambda timeouts is because it is in VPC.您的lambda超时的一个很可能的原因是因为它在 VPC 中。 As such it has no internet access since it does not have public IP.因此它无法访问 Internet ,因为它没有公共 IP。 From docs :来自文档

Connect your function to private subnets to access private resources.将您的 function 连接到私有子网以访问私有资源。 If your function needs internet access, use NAT.如果您的 function 需要互联网访问,请使用 NAT。 Connecting a function to a public subnet does not give it internet access or a public IP address .将 function 连接到公共子网不会为其提供 Internet 访问权限或公共 IP 地址

To rectify the issue, the following should be checked :要纠正该问题,应检查以下内容:

  • is lambda in a private subnet? lambda 在私有子网中吗?
  • is there a NAT gateway/instance in a public subnet?公共子网中是否有 NAT 网关/实例?
  • are route tables correctly configured from private subnet to the NAT device to enable internet access?是否正确配置了从私有子网到 NAT 设备的路由表以启用 Internet 访问?

Alternatively, you may consider not putting it in a VPC in the first place if it is not required.或者,如果不需要,您可以考虑不将其放在 VPC 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM