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.
To troubleshoot the retry and timeout issues, first review the logs of the API call to find the problem. Then, change the retry count and timeout settings of the AWS SDK as needed for each use case. To allow enough time for a response to the API call, add time to the Lambda function timeout setting.
The default timeout of a Lambda function is three seconds. This means, if you don't explicitly configure a timeout, your function invocations will be suspended after three seconds. Now, if you call a few services, some of which are currently at capacity, a request can very well take a second on its own.
Even though a Lambda function's maximum invocation timeout limit is 15 minutes, other AWS services may have different timeout limits. For example, Amazon API Gateway waits a maximum of 29 seconds for a Lambda function proxy invocation to complete.
It’s actually in the name of the function – context.get_remaining_time_in_millis (). If you want to convert the AWS Lambda remaining time to seconds then just divide it by 1,000 to its value in seconds. Here is a Python code that will output the remaining time of Lambda in seconds.
If your Lambda function uses downstream AWS resources, microservices, databases, or HTTP web APIs, then you can use AWS X-Ray to help troubleshoot code performance issues. For more information, see Using AWS Lambda with AWS X-Ray. Lambda Insights collects system-level metrics, including CPU time, memory, disk, and network metrics.
If your lambda function is in a VPC and trying to access the internet, it might time out if you don't have the correct setup, namely: the function has to have permissions to create and manage elastic network interfaces (virtual network cards)
The default value for timeout in Lambda is 3 seconds = 3000 microseconds. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With