-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathebs_waste_analyzer.py
More file actions
57 lines (48 loc) · 1.83 KB
/
ebs_waste_analyzer.py
File metadata and controls
57 lines (48 loc) · 1.83 KB
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
import boto3
from tabulate import tabulate
def analyze_ebs_waste(region='us-east-1', cost_per_gb=0.10):
"""
Identifies EBS volumes in 'available' state and calculates monthly waste.
Args:
region: AWS region to scan (default: us-east-1)
cost_per_gb: Cost per GB per month (default: $0.10)
"""
ec2 = boto3.client('ec2', region_name=region)
print(f"\nScanning EBS volumes in {region}...\n")
# Get all volumes in 'available' state
response = ec2.describe_volumes(
Filters=[
{'Name': 'status', 'Values': ['available']}
]
)
volumes_data = []
total_size = 0
total_waste = 0
for volume in response['Volumes']:
volume_id = volume['VolumeId']
size_gb = volume['Size']
monthly_waste = size_gb * cost_per_gb
volumes_data.append([volume_id, size_gb, f"${monthly_waste:.2f}"])
total_size += size_gb
total_waste += monthly_waste
# Print summary table
if volumes_data:
headers = ['Volume ID', 'Size (GB)', 'Monthly Waste']
print(tabulate(volumes_data, headers=headers, tablefmt='grid'))
print(f"\n{'='*60}")
print(f"Total Volumes: {len(volumes_data)}")
print(f"Total Size: {total_size} GB")
print(f"Total Monthly Waste: ${total_waste:.2f}")
print(f"{'='*60}\n")
else:
print("✓ No unused EBS volumes found in 'available' state.")
print(" All volumes are properly attached!\n")
if __name__ == '__main__':
try:
analyze_ebs_waste()
except Exception as e:
print(f"Error: {e}")
print("\nMake sure you have:")
print("1. AWS credentials configured (aws configure)")
print("2. boto3 installed (pip install boto3)")
print("3. Proper IAM permissions for ec2:DescribeVolumes")