Skip to content

Instantly share code, notes, and snippets.

@kbcaleb
Created April 24, 2019 19:19
Show Gist options
  • Save kbcaleb/d993774758c3ac9ee4ee6c4e110c9b97 to your computer and use it in GitHub Desktop.
Save kbcaleb/d993774758c3ac9ee4ee6c4e110c9b97 to your computer and use it in GitHub Desktop.
import boto3
import json
import sys
import locale
import time
# Currency locale
locale.setlocale( locale.LC_ALL, 'en_US' )
# Profile array
profiles = ['aws-profile1', 'aws-profile2', 'aws-profile3']
for profile in profiles:
# Loop profiles to session
aws = boto3.session.Session(profile_name=profile)
# Boto3 clients
ec2 = aws.client('ec2')
cloudtrail = aws.client('cloudtrail')
# Find all io1 drives
# TODO "NextToken" > 500 results
volumes = ec2.describe_volumes(
Filters=[
{
'Name': 'volume-type',
'Values': [
'io1'
]
}
]
)
# Zero counts
totalSizeSum = 0
totalPiopSum = 0
totalCost = 0
snapTotalSizeSum = 0
snapTotalPiopSum = 0
snapTotalCost = 0
if (len(volumes['Volumes']) != 0):
for volume in volumes['Volumes']:
volumeState = volume['State']
if (volumeState == 'in-use'):
volumeId = volume['VolumeId']
volumeSize = volume['Size']
volumePiop = volume['Iops']
instanceId = volume['Attachments'][0]['InstanceId']
mountPoint = volume['Attachments'][0]['Device']
# CloudTrail will throttle lookups
time.sleep(2)
events = cloudtrail.lookup_events(
LookupAttributes=[
{
'AttributeKey': 'ResourceName',
'AttributeValue': volumeId
}
]
)
# Fetch events that match ID
for event in events['Events']:
# Find CreateVolume events
if (event['EventName'] == "CreateVolume"):
# Load data into JSON
newEvent = json.loads(event['CloudTrailEvent'])
print("========================")
print(profile)
print("Volume Id: ", volumeId)
print("\tMounted to :",instanceId, " as ",mountPoint)
print("\tCreated By: ", newEvent['userIdentity']['arn'])
print("\tVolume Size: ", volumeSize)
print("\tVolume PIOP's: ",volumePiop)
volumeGbCost = volumeSize*.125
volumePiopCost = volumePiop*.065
totalVolumeCost = volumeGbCost+volumePiopCost
print("\tTotal Monthly Cost for Volume: ",locale.currency(totalVolumeCost, grouping=True))
print("\tAnnual Cost for Volume: ",locale.currency(totalVolumeCost*12, grouping=True))
else:
volumeId = volume['VolumeId']
volumeSize = volume['Size']
volumePiop = volume['Iops']
# CloudTrail will throttle lookups
time.sleep(2)
events = cloudtrail.lookup_events(
LookupAttributes=[
{
'AttributeKey': 'ResourceName',
'AttributeValue': volumeId
}
]
)
# Fetch events that match ID
for event in events['Events']:
# Find CreateVolume events
if (event['EventName'] == "CreateVolume"):
# Load data into JSON
newEvent = json.loads(event['CloudTrailEvent'])
print("========================")
print(profile)
print("Volume Id: ", volumeId)
print("\tCreated By: ", newEvent['userIdentity']['arn'])
print("\tVolume Size: ", volumeSize)
print("\tVolume PIOP's: ",volumePiop)
volumeGbCost = volumeSize*.125
volumePiopCost = volumePiop*.065
totalVolumeCost = volumeGbCost+volumePiopCost
print("\tTotal Monthly Cost for Volume: ",locale.currency(totalVolumeCost, grouping=True))
print("\tAnnual Cost for Volume: ",locale.currency(totalVolumeCost*12, grouping=True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment