-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathaws-pricing-query.py
106 lines (83 loc) · 4.7 KB
/
aws-pricing-query.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#! /usr/local/bin/python3
import boto3
import json
import re
location = "US West (Oregon)" # AWS GovCloud (US), Asia Pacific (Mumbai), Asia Pacific (Seoul), Asia Pacific (Singapore), Asia Pacific (Sydney), Asia Pacific (Tokyo), Canada (Central), EU (Frankfurt), EU (Ireland), EU (London), EU (Paris), South America (Sao Paulo), US East (N. Virginia), US East (Ohio), US West (N. California), US West (Oregon)
tenancy = "Shared" # Shared, Dedicated
instanceType = "m5.xlarge" # General Purpose Instance Family/Models
operatingSystem = "RHEL" # Generic, Linux, NA, RHEL, SUSE, Windows
preInstalledSw = "NA" # NA, SQL Ent, SQL Std, SQL Web
licenseModel = "No License required" # Bring your own license, NA, No License required
volumeType = "General Purpose" # Cold HDD, General Purpose, Magnetic, Provisioned IOPS, Throughput Optimized HDD
pricing = boto3.client('pricing')
# EC2 Instance Attribute Match
instanceData = pricing.get_products(
ServiceCode='AmazonEC2',
Filters = [
{'Type' :'TERM_MATCH', 'Field':'location', 'Value':location },
{'Type' :'TERM_MATCH', 'Field':'tenancy', 'Value':tenancy },
{'Type' :'TERM_MATCH', 'Field':'instanceType', 'Value':instanceType },
{'Type' :'TERM_MATCH', 'Field':'operatingSystem', 'Value':operatingSystem },
{'Type' :'TERM_MATCH', 'Field':'preInstalledSw', 'Value':preInstalledSw },
{'Type' :'TERM_MATCH', 'Field':'licenseModel', 'Value':licenseModel },
],
#MaxResults=100
)
# Return output from pricing API
#instanceString = str(instanceData)
#print (instanceString)
# When the Capacity Reservation is active, you are charged the equivalent On-Demand rate whether you run the instances or not.
# If you do not use the reservation, this shows up as unused reservation on your EC2 bill.
# When you run an instance that matches the attributes of a reservation, you just pay for the instance and nothing for the reservation.
#AllocatedCapacityReservation
#UnusedCapacityReservation
#Used = OnDemand Pricing
for instanceVal in instanceData["PriceList"]:
instanceValJson=json.loads(instanceVal)
if("OnDemand" in instanceValJson["terms"] and len(instanceValJson["terms"]["OnDemand"]) > 0):
for onDemandValues in instanceValJson["terms"]["OnDemand"].keys():
for priceDimensionValues in instanceValJson["terms"]["OnDemand"][onDemandValues]["priceDimensions"]:
if("Used" in instanceValJson["product"]["attributes"]["capacitystatus"]):
instancePrice = (instanceValJson["terms"]["OnDemand"][onDemandValues]["priceDimensions"][priceDimensionValues]["pricePerUnit"])
instanceString = str(instancePrice)
#print (instanceString)
instanceUnitPrice = re.search( r'(\d{1,10}\.\d{1,10})', instanceString, re.S)
#print (instanceUnitPrice)
instanceUnitPrice = instanceUnitPrice.group(1)
print ("Instance Unit Price: " + (instanceUnitPrice))
instanceHourlyPrice = float(instanceUnitPrice)
print ("Instance Hourly Price: " + str(instanceHourlyPrice))
instanceDailyPrice = (float(instanceUnitPrice) * 24)
print ("Instance Daily Price: " + str(instanceDailyPrice))
# EBS Storage Attribute Match
storageData = pricing.get_products(
ServiceCode='AmazonEC2',
Filters = [
{'Type' :'TERM_MATCH', 'Field':'location', 'Value':location },
{'Type' :'TERM_MATCH', 'Field':'volumeType', 'Value':volumeType },
],
#MaxResults=100
)
# Return output from pricing API
storageString = str(storageData)
#print (storageString)
for storageVal in storageData["PriceList"]:
storageValJson=json.loads(storageVal)
if("OnDemand" in storageValJson["terms"] and len(storageValJson["terms"]["OnDemand"]) > 0):
for onDemandValues in storageValJson["terms"]["OnDemand"].keys():
for priceDimensionValues in storageValJson["terms"]["OnDemand"][onDemandValues]["priceDimensions"]:
storagePrice = (storageValJson["terms"]["OnDemand"][onDemandValues]["priceDimensions"][priceDimensionValues]["pricePerUnit"])
storageString = str(storagePrice)
#print (storageString)
storageUnitPrice = re.search( r'(\d{1,10}\.\d{1,10})', storageString, re.S)
#print (storageUnitPrice)
storageUnitPrice = storageUnitPrice.group(1)
print ("Storage Unit Price: " + (storageUnitPrice))
# Multiply storageHourlyPrice by GB
storageHourlyPrice = ((float(storageUnitPrice) * 3600) / (86400 * 30))
storageHourlyPrice = round(storageHourlyPrice,10)
print ("Storage Hourly Price: " + str(storageHourlyPrice))
# Multiply storageDailyPrice by GB
storageDailyPrice = ((float(storageUnitPrice) * 86400) / (86400 * 30))
storageDailyPrice = round(storageDailyPrice,10)
print ("Storage Daily Price: " + str(storageDailyPrice))