AWS Lambda: Get current retry attempt count at runtime

1.7k Views Asked by At

I have an AWS Lambda (python3.7 runtime) that runs certain task, and its MaximumRetryAttempts is set to 1 (meaning it can fail once, then have 1 more try).

For a reasons I won't get into, I want to know, at runtime, if this is the first attempt or the second. Is there any way to achieve that?

1

There are 1 best solutions below

2
amitd On
import json
import boto3
from datetime import datetime, timedelta
import time

aws_cloud_watch_logs_client = boto3.client('logs')
lambda_invocation_count_query = "fields @retry_count | filter @message like 'REPORT' AND @requestId = '{lambda_request_id}' | stats count(@message) as retry_count"
aws_cloud_watch_log_group = '/aws/lambda/mylambdaname' # Set aws cloudwatch log group name as per your setup

def lambda_handler(event, context):
    lambda_retry_counts=get_lambda_retry_count(context)
    print("lambda_retry_counts=",lambda_retry_counts)
    #TODO : your logic here
    

# refactor to reduce complexity, might extract method to cover single responsibility principle
def get_lambda_retry_count(context):
    print("context.aws_request_id=", context.aws_request_id)
    qry=lambda_invocation_count_query.format(lambda_request_id=context.aws_request_id)
    print(qry)
    start_query_response = aws_cloud_watch_logs_client.start_query(
    logGroupName=aws_cloud_watch_log_group,
    startTime=int((datetime.today() - timedelta(hours=1)).timestamp()), # Refactor as per your requirement
    endTime=int(datetime.now().timestamp()),
    queryString=qry,
    )

    query_id = start_query_response['queryId']

    response = None

    while response == None or response['status'] == 'Running':
        print('Waiting for cloud watch query to complete ...')
        time.sleep(10) # Refactor as per your requirement
        response = aws_cloud_watch_logs_client.get_query_results(
            queryId=query_id
        )
    
    if response['results']:
        return response['results'][0][0]['value'] # refactor for readability, maintenance
    else:
        return 0
  

Alternative, To query aws cloud watch, you can also use AWS System Parameter Store with aws_request_id as part of Parameter name to uniquely identify it. Furthermore, keep lambda retry count as value of this Parameter. Increment it per execution of lambda based on aws_request_id. If threshold reach then handle as per your logic and delete the AWS System Parameter Store with name having the aws_request_id for which threshold is reached. Summary- you just need some storage which keeps mapping of aws_request_id and retry count.