Skip to content

Instantly share code, notes, and snippets.

@nicbor
Forked from Eyjafjallajokull/README.md
Last active October 23, 2020 16:27
Show Gist options
  • Save nicbor/14987b2a382a384fd6299f6a76def099 to your computer and use it in GitHub Desktop.
Save nicbor/14987b2a382a384fd6299f6a76def099 to your computer and use it in GitHub Desktop.
AWS EBS - Find unused snapshots - this script generates csv raport about snapshot usage
import re
import boto3
import csv
from botocore.exceptions import ClientError
ec2 = boto3.client('ec2')
def get_snapshots():
return ec2.describe_snapshots(OwnerIds=['self'])['Snapshots']
def volume_exists(volume_id):
if not volume_id: return ''
try:
ec2.describe_volumes(VolumeIds=[volume_id])
return True
except ClientError:
return False
def instance_exists(instance_id):
if not instance_id: return ''
try:
ec2.describe_instances(InstanceIds=[instance_id])
return True
except ClientError:
return False
def image_exists(image_id):
if not image_id: return ''
try:
requestObj = ec2.describe_images(ImageIds=[image_id,])
if not requestObj["Images"]:
return False
return True
except ClientError:
return False
def parse_description(description):
regex = r"^Created by CreateImage\((.*?)\) for (.*?) "
matches = re.finditer(regex, description, re.MULTILINE)
for matchNum, match in enumerate(matches):
return match.groups()
return '', ''
def main():
with open('raport.csv', 'w') as csvfile:
writer = csv.writer(csvfile)
writer.writerow([
'snapshot id',
'description',
'started',
'size',
'volume',
'volume exists',
'instance',
'instance exists',
'ami',
'ami exists'])
for snap in get_snapshots():
instance_id, image_id = parse_description(snap['Description'])
writer.writerow([
snap['SnapshotId'],
snap['Description'],
snap['StartTime'],
str(snap['VolumeSize']),
snap['VolumeId'],
str(volume_exists(snap['VolumeId'])),
instance_id,
str(instance_exists(instance_id)),
image_id,
str(image_exists(image_id)),
])
if __name__ == '__main__':
main()
@RajenAN
Copy link

RajenAN commented Oct 25, 2017

@anissinen I have updated the code based on your suggestion . Updated code below

import re
import boto3
import csv
from botocore.exceptions import ClientError

ec2 = boto3.client('ec2')

def get_snapshots():
return ec2.describe_snapshots(OwnerIds=['self'])['Snapshots']

def volume_exists(volume_id):
if not volume_id: return ''
try:
ec2.describe_volumes(VolumeIds=[volume_id])
return True
except ClientError:
return False

def instance_exists(instance_id):
if not instance_id: return ''
try:
ec2.describe_instance_status(InstanceIds=[instance_id])
return True
except ClientError:
return False

def image_exists(image_id):
if not image_id: return ''
try:
requestObj = ec2.describe_images(ImageIds=[image_id,])
if not requestObj["Images"]:
return False
return True
except ClientError:
return False

def parse_description(description):
regex = r"^Created by CreateImage((.?)) for (.?) "
matches = re.finditer(regex, description, re.MULTILINE)
for matchNum, match in enumerate(matches):
return match.groups()
return '', ''

def main():
with open('report.csv', 'w') as csvfile:
writer = csv.writer(csvfile)
writer.writerow([
'snapshot id',
'description',
'started',
'size',
'volume',
'volume exists',
'instance',
'instance exists',
'ami',
'ami exists'])
for snap in get_snapshots():
instance_id, image_id = parse_description(snap['Description'])
writer.writerow([
snap['SnapshotId'],
snap['Description'],
snap['StartTime'],
str(snap['VolumeSize']),
snap['VolumeId'],
str(volume_exists(snap['VolumeId'])),
instance_id,
str(instance_exists(instance_id)),
image_id,
str(image_exists(image_id)),
])

if name == 'main':
main()`

@Leonidze
Copy link

Leonidze commented Mar 26, 2018

Hi.
Thank you!
Unfortunately your code is not quite expected to work:

> 
#py -3 ./ snapshots.py
  File "snapshot_3.py", line 76
    main()`
          ^
SyntaxError: invalid syntax
> 

This works after a replacement on:

if __name__ == '__main__':
    main()

But why then in the csv-report empty columns: instance, instance exists, ami, ami exists?
Could you, please , take a look?
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment