簡體   English   中英

使用 Python 在 Lambda 中獲取請求任務超時

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

我是 AWS 和 Python 的新手。 我正在嘗試通過 Lambda 獲取 HTTP 請求的結果。 我附加了包含請求庫的層。

這是問題所在:

運行時: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

我得到的是

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

當我從 Talend API 測試儀發送此請求時,響應返回正確。

我必須更改測試事件的參數嗎? 這有關系嗎? 當前內容是

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

我在這里想念什么? 請告訴我是否需要添加更多信息。 提前致謝!

您的lambda超時的一個很可能的原因是因為它在 VPC 中。 因此它無法訪問 Internet ,因為它沒有公共 IP。 來自文檔

將您的 function 連接到私有子網以訪問私有資源。 如果您的 function 需要互聯網訪問,請使用 NAT。 將 function 連接到公共子網不會為其提供 Internet 訪問權限或公共 IP 地址

要糾正該問題,應檢查以下內容:

  • lambda 在私有子網中嗎?
  • 公共子網中是否有 NAT 網關/實例?
  • 是否正確配置了從私有子網到 NAT 設備的路由表以啟用 Internet 訪問?

或者,如果不需要,您可以考慮不將其放在 VPC 中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM