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!

6
  • 1
    What happens when you run this code locally (provided you can access the url locally)? Usually when something times out I immediately think of a network issue. By default the security groups (these are basically AWS firewall rules) for lambda allow outgoing traffic but it might be changed in your case? Commented Jun 24, 2020 at 6:39
  • Is your function in a VPC? Commented Jun 24, 2020 at 6:44
  • Default memory and execution time/Timeout settings please Commented Jun 24, 2020 at 6:49
  • @Ludo21South It think I can't run the code locally..as for the security group, does outgoing traffic mean oubound rules? type,protocol,port,they are all set "all". Commented Jun 24, 2020 at 7:13
  • 1
    @Marcin yes,in a VPC. Commented Jun 24, 2020 at 7:14

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.