0

I need to get the same information that in This post,this is, when a lambda was last executed (invoked). But I have to do it with more than 100 lambdas so I can't do it manually. I've been trying to use AWS CLI, but the lambda options don't get this info. I also tried getting CW logs with boto3, but It requires the log group and the log stream (this last one is the data I need). I can't seem to be able to get this information with anything scripted. Any ideas?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Naxxio
  • 63
  • 1
  • 10
  • You can try an approach similar to the one answered here: https://stackoverflow.com/questions/59240107/how-to-query-cloudwatch-logs-using-boto3-in-python Just make some changes to the query, have a list of your function names, do some looping and extract the last execution times. – ms12 Sep 28 '22 at 21:07
  • Thanks @newbie, that did the trick. I'll share the code I used on my answer – Naxxio Sep 29 '22 at 19:32

1 Answers1

0

As @newbie said, the solution in that post was helpful. This is how I change the code since I only needed the last execution:

    import boto3
    
    # as the name shows, this is the file where I have all the lambdas, by name
    with open("lambdas-list.txt", "r") as f:  
        lines = f.readlines()
    
        # This is just a loop to make a call for each lambda
        for line in lines:
            lambda_name = line.rstrip("\n")
    
            log_g_name = "/aws/lambda/" + lambda_name
    
            # had to add the region here to make it work
            client = boto3.client('logs', region_name='us-east-1')
    
            try:
                ## For the latest
                stream_response = client.describe_log_streams(
                    logGroupName= log_g_name,  # Can be dynamic
                    orderBy='LastEventTime',                 # For the latest events
                    limit=1                                  # the last latest event, if you just want one
                    )
    
                print(stream_response)
                
            except:
                print("Lambda not found:", log_g_name)
Pandapip1
  • 730
  • 5
  • 15
Naxxio
  • 63
  • 1
  • 10