0

I'm new to AWS and Python. I'm trying to get the results of HTTP requests through Lambda. I attached the layer which includes requests library.

Here is the problem:

Runtime: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.

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!

1 Answers1

2

A very likely reason why your lambda timeouts is because it is in VPC. As such it has no internet access since it does not have public IP. From docs:

Connect your function to private subnets to access private resources. If your function needs internet access, use NAT. Connecting a function to a public subnet does not give it internet access or a public IP address.

To rectify the issue, the following should be checked:

  • is lambda in a private subnet?
  • is there a NAT gateway/instance in a public subnet?
  • are route tables correctly configured from private subnet to the NAT device to enable internet access?

Alternatively, you may consider not putting it in a VPC in the first place if it is not required.

Marcin
  • 215,873
  • 14
  • 235
  • 294