-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrotation_storagegateway_volume.py
More file actions
38 lines (29 loc) · 1.14 KB
/
rotation_storagegateway_volume.py
File metadata and controls
38 lines (29 loc) · 1.14 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
import commands, boto3
def _(cmd):
return commands.getoutput(cmd)
def search_snapshots(volume_name):
#client = boto3.client('sts')
#account = client.get_caller_identity()['Account']
snapshots = boto3.client('ec2').describe_snapshots(OwnerIds=['************',])["Snapshots"]
targetSnapshots = []
for snapshot in sorted(snapshots, key=lambda x: x["StartTime"]):
try:
for tag in snapshot["Tags"]:
if (tag["Key"] == "Name" and tag["Value"] == volume_name):
targetSnapshots.append(snapshot)
except:
print "no Tag!"
return targetSnapshots
def delete_snapshots(targetSnapshots, deleteCount):
count = deleteCount
ec2 = boto3.resource('ec2')
for targetSnapshot in targetSnapshots:
if count > 0:
ec2.Snapshot(targetSnapshot["SnapshotId"]).delete()
count -= 1
else:
return
def lambda_handler(event, context):
targetSnapshots = search_snapshots(event['volume_name'])
deleteCount = len(targetSnapshots) - int(event['generation_number'])
delete_snapshots(targetSnapshots,deleteCount)