I need to delete the snapshot of my Elastic Block Store volume whose EBS volumes are deleted. I would like to do this using a Lambda function. I wrote a script which will give me false if the EBS volume does not exist. How can I modify it to delete any related snapshots?
def get_snapshots():
account_ids = list()
account_ids.append( boto3.client('sts').get_caller_identity().get('Account'))
return ec2.describe_snapshots(OwnerIds=account_ids)
def volume_exists(volume_id):
if not volume_id: return ''
try:
ec2.describe_volumes(VolumeIds=[volume_id])
return True
except ClientError:
return False
def lambda_handler(event, context):
with open('/tmp/report.csv', 'w') as csvfile:
writer = csv.writer(csvfile)
writer.writerow([
'volume exists'
])
snaps = get_snapshots()
for snap in snaps.get('Snapshots'):
writer.writerow([
str(volume_exists(snap['VolumeId']))
])
Any suggestion?