Skip to content

Instantly share code, notes, and snippets.

@danilop
Created December 5, 2023 12:24
Show Gist options
  • Save danilop/c2d829f54c4a0b6830dde0b49354fb28 to your computer and use it in GitHub Desktop.
Save danilop/c2d829f54c4a0b6830dde0b49354fb28 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import boto3
import argparse
from operator import itemgetter
from collections import defaultdict
def nested_defaultdict():
return defaultdict(nested_defaultdict)
def get_free_tier_usage(verbose = False):
paginator = freetier.get_paginator("get_free_tier_usage")
free_tier_usage = []
for page in paginator.paginate():
free_tier_usage.extend(page["freeTierUsages"])
formatted_usage = nested_defaultdict()
for usage in free_tier_usage:
if not formatted_usage[usage['freeTierType']][usage['service']][usage['description']]:
formatted_usage[usage['freeTierType']][usage['service']][usage['description']] = []
formatted_usage[usage['freeTierType']][usage['service']][usage['description']].append(usage)
for freeTierType in sorted(formatted_usage.keys()):
print()
print(freeTierType)
print('=' * len(freeTierType))
for service in sorted(formatted_usage[freeTierType].keys()):
print()
print(service)
print('-' * len(service))
for description in sorted(formatted_usage[freeTierType][service].keys()):
for usage in formatted_usage[freeTierType][service][description]:
if usage['forecastedUsageAmount'] > usage['limit']:
check_usage = 'CHECK > '
else:
check_usage = ''
if verbose:
output_description = '- ' + description
else:
output_description = ''
print(
"{}{} {} / {} ({}) {} {}".format(
check_usage,
#usage['operation'] or '-',
#usage['usageType'],
usage['region'],
usage['actualUsageAmount'],
usage['limit'],
usage['forecastedUsageAmount'],
usage['unit'],
output_description,
),
)
def build_parser():
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--verbose', action='store_true')
return parser
freetier = boto3.client("freetier")
if __name__ == "__main__":
parser = build_parser()
args = parser.parse_args()
get_free_tier_usage(args.verbose)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment