Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sort lambda functions by invocations
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