I have a lambda that:
The problem is that it fails quite often but succeeds on retry. It triggers Rollbar that I have to go and check by hand.
I wondered if there was a way to record a Rollbar ONLY if this is the last of 3 retries and it will go to DLQ now.
There is ApproximateReceiveCount that seems to work but I'm concerned about it having Approximate in its name
This is an example of how I thought of doing that
@rollbar.lambda_function
def handler(event, context):
approximate_receive_count = event['Records'][0]['attributes']['ApproximateReceiveCount']
if int(approximate_receive_count) < MAX_RECEIVE_COUNT:
raise Exception("Simulate transient server failure")
if int(approximate_receive_count) >= MAX_RECEIVE_COUNT:
raise Exception('Simulate final failure')
return {
'statusCode': 200,
'body': json.dumps({'message': 'Lambda function executed successfully.'})
}
I think it's called ApproximateReceiveCount
because SQS does not guarantee only-once delivery and the same message could be in some edge cases picked up by two lambdas at the same time (in which case both would have the same value for ApproximateReceiveCount
. If you really cannot tolerate these cases then you can't use ApproximateReceiveCount
attribute. For precise solution, you could: