Search code examples
pythontimeoutaws-lambdabotoboto3

invoking lambda with boto doesn't respect timeout


I'm invoking a lambda function with boto3, with:

import boto3
import json

client = boto3.client('lambda')
response = client.invoke(
            InvocationType='RequestResponse',
            FunctionName=test_lambda_arn,
            LogType='Tail',
            Payload=json.dumps(data)
)

It's raising a botocore.vendored.requests.exceptions.ReadTimeout exception after 626 seconds. I have configured this lambda to time out after 100 seconds. I did so when I created it with boto. When I go into the AWS console it says that this lambda has a timeout of 1 minute and 40 seconds.

So why does the invoke command timeout after 626 seconds, and not after 100 seconds?

Is it because of retries? If so, how can I disable retries?

Edit: In the CloudWatch logs I can see multiple invocations for each client.invoke call. Therefore there is some automatic retry thing happening. Here are the docs for client.invoke.


Solution

  • Create a botocore config object with a longer read_timeout value (and possibly other things), and pass it in when creating your lambda client:

    import botocore
    import boto3
                
    cfg = botocore.config.Config(retries={'max_attempts': 0}, read_timeout=840, connect_timeout=600, region_name="us-east-1" )
               
    client = boto3.client(
        'lambda', config=cfg, region_name="us-east-1",
         aws_access_key_id="*********", aws_secret_access_key="*********")
    
    payload = {"input_array": input_arr}
    
    result = client.invoke(
         FunctionName="**********", InvocationType='RequestResponse',
         LogType='Tail', Payload=json.dumps(payload,cls=NumpyArrayEncoder))
            
    
    range = result['Payload'].read()
    range_json = json.loads(range)