Python query in AWS Lambda time out

I am trying to make an http request from my AWS Lambda, but this time.

My code is similar 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 check this, I get a timeout.

Output signal

 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 that the problem is not to find the request package, but that it runs my Python code. I'm just figuring out why this time is running out on this request.

+6
source share
5 answers

I am facing the same timeout problem , reason below.

AWS Document :

When you add a VPC configuration to a Lambda function, it can only access resources in that VPC. If Lambda features need to access VPC resources and the public Internet, the VPC must have an instance of Network Address Translation (NAT) inside the VPC.

There may be some error setting up the VPC. I advise you to follow this blog to create NAT.

+3
source

The Lambda feature with VPC access will not have access to the Internet unless you add a NAT gateway to the VPC. You should read the What You Need to Know section of the Lambda VPC post.

If you have enabled VPC support for your lambda function, but you do not have a NAT gateway in your VPC, then your request is disconnected trying to access the Internet.

+4
source

You can increase the wait time for a request by doing the following:

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

In addition, you need to increase the waiting period for your lambda function. For this:

  • Open your lambda function in AWS
  • Select "Configuration", then "Advanced Settings"
  • Increase the waiting time (up to 5 minutes).
  • Choose save

In addition, I believe request.get should be "request.get".

+1
source

The default value for a timeout in Lambda is 3 seconds = 3000 microseconds. Go to the "Advanced Settings" section and add 5 minutes. This may be the only problem if the timeout occurs after exactly 3 seconds. All other errors take up more or less.

0
source

By default, AWS Lambda has a 3 second timeout, so if your code runs for more than 3 seconds, it will automatically disable this error.

You can increase the wait time for your lambda function to 5 minutes (300 seconds - AWS may increase this limit in the future), and this should take care of the problem.

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

0
source

All Articles