Skip to content

Instantly share code, notes, and snippets.

@toabctl
Created December 6, 2022 09:18
Show Gist options
  • Save toabctl/eb29eb545f34f29d5ab74d7c6489ef32 to your computer and use it in GitHub Desktop.
Save toabctl/eb29eb545f34f29d5ab74d7c6489ef32 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import boto3
ec2res = boto3.resource('ec2')
ec2client = boto3.client('ec2')
region_name = ec2client.meta.region_name
snapshots = ec2res.snapshots.filter(OwnerIds=['self'])
snapshots_size_total = 0
snapshots_size_unused = 0
snapshots_count_total = 0
snapshots_count_unused = 0
for s in snapshots:
# get ami for snapshot
filters_ebs = [{"Name": "block-device-mapping.snapshot-id", "Values": [s.id]}]
images_ebs = [i['ImageId'] for i in ec2client.describe_images(Filters=filters_ebs)["Images"]]
snapshots_size_total += s.volume_size
snapshots_count_total += 1
if len(images_ebs) == 0:
print(f'{region_name}: {s.id} ({s.volume_size} GiB, {s.start_time}, {s.description}) is not used by any image')
snapshots_count_unused += 1
snapshots_size_unused += s.volume_size
print(f'{region_name}: Found {snapshots_count_total} total snapshots ({snapshots_size_total} GiB)')
print(f'{region_name}: Found {snapshots_count_unused} unused snapshots ({snapshots_size_unused} GiB)')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment