Python request in AWS Lambda timing out

Question:

I’m trying to make a http request from my AWS Lambda but it times out.

My code looks similiar to this:

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.

The output is

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

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.

Asked By: user133688

||

Answers:

A Lambda function with VPC access will not have internet access unless you add a NAT gateway to your VPC. You should read the “Things to Know” section of the Lambda VPC support announcement.

If you’ve enabled VPC support for your Lambda function, but don’t have a NAT gateway in your VPC, then your request is timing out trying to access the internet.

Answered By: Mark B

You can increase the timeout period for the request by doing the following:

response = requests.get(url, timeout=60)

Also, you will need to increase the timeout period for your lambda function. To do this:

  • Open your lambda function in AWS
  • Select ‘Configuration’, then ‘Advanced Settings’
  • Increase your time out period (up to 5 minutes)
  • Select ‘Save’

Also, I believe ‘request.get’ should be ‘requests.get’.

Answered By: apoclyps

I encounter same timeout problem, the reason is below.

AWS document:

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.

Maybe there are some error when you setting your VPC.
I advice you can follow this blog to build NAT.

Answered By: Jim

The default value for timeout in Lambda is 3 seconds = 3,000 milliseconds.
Go to Advanced settings and add 5 min.
This could be the only issue, if the timeout happens exactly at 3 seconds. All other errors would take something more or less than that.

Answered By: pyBomb

By default, AWS Lambda has 3 seconds of timeout so if your code is running for more than 3 seconds, it will automatically timeout giving you this error.

You can increase the timeout for your lambda function to up to 5 mins (300 seconds – AWS may increase this limit in the future) and that should take care of the problem.

Plus your code should reflect requests.get instead of request.get

Answered By: Abhineetraj

I have configured VPC configuration in Lambda function and also added the NAT gateway, set the function time to 5 minutes.

Still the problem timed out after XX-- seconds existed. By setting context.callbackWaitsForEmptyEventLoop to false solved the problem for me.

Answered By: Brotherhood

I had this issue as well. If you are running lambda outside of a VPC, make sure you are in a Region that supports SES. If the region doesn’t support it the lambda functions are always going to fail.

Answered By: Zebula