Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save james-jory/fd508a2fa714ad2e5c9295f860336a0c to your computer and use it in GitHub Desktop.
Save james-jory/fd508a2fa714ad2e5c9295f860336a0c to your computer and use it in GitHub Desktop.
Calculates total training hours cost (for all time) for one or more Amazon Personalize solutions
'''
Calculates total training hours cost (for all time) for one or more Amazon Personalize solutions.
Total training hours and costs across AWS regions as well as grand totals will be printed as output.
By default, the script will calculate the costs for all solutions in the default AWS region (as
configured in your local environment). Alternatively, you can pass a filename as an argument to
the script where the filename points to a text file containing one or more Amazon Personalize
solution ARNs (1 per line). The solution ARNs in the file can come from multiple regions.
'''
import sys
import boto3
solution_count = 0
solutions_by_region = {}
if len(sys.argv) > 1:
filename = sys.argv[1]
print('Loading solution versions for solution ARNs in {}'.format(filename))
with open(filename) as file:
for solution_arn in file:
arn_bits = solution_arn.split(':')
region = arn_bits[3]
solution_count += 1
if not solutions_by_region.get(region):
solutions_by_region[region] = [ solution_arn.strip() ]
else:
solutions_by_region[region].append(solution_arn.strip())
else:
print('Loading all solution versions for default region')
personalize = boto3.client('personalize')
paginator = personalize.get_paginator('list_solutions')
for paginate_result in paginator.paginate():
for solution in paginate_result["solutions"]:
solution_arn = solution['solutionArn']
arn_bits = solution_arn.split(':')
region = arn_bits[3]
solution_count += 1
if not solutions_by_region.get(region):
solutions_by_region[region] = [ solution_arn.strip() ]
else:
solutions_by_region[region].append(solution_arn.strip())
print('Loaded {} solutions for {} regions'.format(solution_count, len(solutions_by_region.keys())))
# From https://aws.amazon.com/personalize/pricing/
cost_per_training_hour = 0.24
total_training_hours = 0.0
for region in solutions_by_region.keys():
print('Totalling training hours for solutions in {}'.format(region))
personalize = boto3.client(service_name = 'personalize', region_name = region)
region_training_hours = 0.0
for solution_version_arn in solutions_by_region[region]:
paginator = personalize.get_paginator('list_solution_versions')
for paginate_result in paginator.paginate(solutionArn = solution_version_arn):
for solution_version in paginate_result["solutionVersions"]:
describe_response = personalize.describe_solution_version(solutionVersionArn = solution_version["solutionVersionArn"])
status = describe_response['solutionVersion']['status']
if status == 'ACTIVE':
training_hours = describe_response['solutionVersion']['trainingHours']
region_training_hours += training_hours
total_training_hours += training_hours
else:
print('Solution version {} status is {}'.format(solution_version["solutionVersionArn"], status))
print('Training hours for {}: {:.4f}'.format(region, total_training_hours))
print('Training cost for {}: $ {:.2f}'.format(region, total_training_hours * cost_per_training_hour))
print('')
print('Total training hours: {:.4f}'.format(total_training_hours))
print('Total training cost: $ {:.2f}'.format(total_training_hours * cost_per_training_hour))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment