Created
October 3, 2019 10:44
-
-
Save Sam-Martin/ef49e1251c3386db41f3d67066c9ce58 to your computer and use it in GitHub Desktop.
Sort lambda functions by invocations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import boto3 | |
import datetime | |
cw = boto3.client('cloudwatch') | |
list_metrics_paginator = cw.get_paginator('list_metrics') | |
function_stats = [] | |
for page in list_metrics_paginator.paginate(Namespace='AWS/Lambda', MetricName='Invocations'): | |
for metric in page['Metrics']: | |
if not len(metric['Dimensions']) == 1: | |
continue | |
function_name = next( | |
iter([ | |
x['Value'] | |
for x in metric['Dimensions'] | |
if x['Name'] == 'FunctionName' | |
]) | |
) | |
sys.stdout.write('.') | |
sys.stdout.flush() | |
stats_args = {**metric, **{ | |
'StartTime': datetime.datetime.utcnow() - datetime.timedelta(hours=1), | |
'EndTime': datetime.datetime.utcnow(), | |
'Period': 60*60, | |
'Statistics': ['Sum'] | |
}} | |
stats = cw.get_metric_statistics(**stats_args) | |
sum = int(next(iter(stats['Datapoints'] or [{'Sum': 0}]))['Sum']) | |
function_stats.append( | |
{'FunctionName': function_name, 'Invocations': sum}) | |
print('') | |
for function in sorted(function_stats, key=lambda function: function['Invocations']): | |
print(function) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment