18

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.

2
  • Did you enable VPC access for your Lambda function? Commented Apr 8, 2016 at 20:48
  • Yep, VPC access enabled. Commented Apr 8, 2016 at 20:50

7 Answers 7

27

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.

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

Comments

21

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.

2 Comments

Welcome to StackOverflow and thanks for your help. You might want to make your answer even better by adding some explanation.
The steps are: In AWS Console go inside the Lambda function -> Click 'Configuration' tab -> click 'Edit' -> Set 'Timeout' to the necessary value.
9

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.

3 Comments

Added a NAT Gateway to the VPC and a route for 0.0.0.0 through that NAT but still missing something. Timeouts still occur.
I wonder if the NAT is still required if a Public Subnet is specified?
Yes it is still required because the Lambda function doesn't get a public IP address.
5

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'.

Comments

1

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

2 Comments

where can you do this? couldn't find the place
The timeout is set in the lambda page for that function. If you scroll down, under Basic Settings.
0

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.

Comments

-1

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.

1 Comment

does this also exist for Python as the question is about Python

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.