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()
@anissinen
Copy link

Excellent tool and modifications!
A small improvement proposal:
Now the existence of an instance is concluded from the fact that the description is returned without error. In the case of terminated VM the machine hangs on the list for a while and the report indicates that the instance exists even if it is already terminated.

Checking the state might be usefull when determining the existence of instance.
u'State': {u'Code': 48, u'Name': 'terminated'},

{u'Reservations': [{u'OwnerId': '949011262558', u'ReservationId': 'r-008f61d73fc9c230c', u'Groups': [], u'Instances': [{u'Monitoring': {u'State': 'disabled'}, u'PublicDnsName': '', u'RootDeviceType': 'ebs', u'State': {u'Code': 48, u'Name': 'terminated'}, u'EbsOptimized': False, u'LaunchTime': datetime.datetime(2017, 8, 21, 10, 18, 58, tzinfo=tzutc()), u'ProductCodes': [], u'StateTransitionReason': 'User initiated (2017-08-21 10:26:54 GMT)', u'InstanceId': 'i-01d317ad20f25295e', u'EnaSupport': True, u'ImageId': 'ami-1e749f67', u'PrivateDnsName': '', u'KeyName': 'VII_trenissian', u'SecurityGroups': [], u'ClientToken': 'TKVyW1503310737236', u'InstanceType': 't2.micro', u'NetworkInterfaces': [], u'Placement': {u'Tenancy': 'default', u'GroupName': '', u'AvailabilityZone': 'eu-west-1a'}, u'Hypervisor': 'xen', u'BlockDeviceMappings': [], u'Architecture': 'x86_64', u'StateReason': {u'Message': 'Client.UserInitiatedShutdown: User initiated shutdown', u'Code': 'Client.UserInitiatedShutdown'}, u'RootDeviceName': '/dev/sda1', u'VirtualizationType': 'hvm', u'Tags': [{u'Value': 'snapshot tester 1', u'Key': 'nimi'}, {u'Value': 'tester 1', u'Key': 'Name'}], u'AmiLaunchIndex': 0}]}], 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': 'ef09628d-bdd2-49c4-8bd3-44069af462aa', 'HTTPHeaders': {'transfer-encoding': 'chunked', 'vary': 'Accept-Encoding', 'server': 'AmazonEC2', 'content-type': 'text/xml;charset=UTF-8', 'date': 'Mon, 21 Aug 2017 10:51:42 GMT'}}}

@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