-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_AMI_weekly.py
More file actions
124 lines (95 loc) · 4.53 KB
/
lambda_AMI_weekly.py
File metadata and controls
124 lines (95 loc) · 4.53 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import boto3
import collections
import datetime
import sys
import pprint
import time
import json
ec = boto3.client('ec2')
asg = boto3.client('autoscaling')
def lambda_handler(event, context):
reservations = ec.describe_instances(
Filters=[
{'Name': 'tag-key', 'Values': ['Backup', 'backup']},
{'Name': 'tag-value', 'Values': ['Weekly', 'weekly']},
]
).get(
'Reservations', []
)
instances = sum(
[
[i for i in r['Instances']]
for r in reservations
], [])
print "Found %d instances that need a weekly back up" % len(instances)
to_tag = collections.defaultdict(list)
device = collections.defaultdict(list)
names = collections.defaultdict(list)
for instance in instances:
try:
retention_days = [
int(t.get('Value')) for t in instance['Tags']
if t['Key'] == 'Retention'][0]
except IndexError:
retention_days = 7
name = [
str(t.get('Value')) for t in instance['Tags']
if t['Key'] == 'Name']
name = str(name)
name = name.strip( "[']" )
desc = [
str(t.get('Value')) for t in instance['Tags']
if t['Key'] == 'Description']
desc = str(desc)
desc = desc.strip( "[']" )
#for dev in instance['BlockDeviceMappings']:
# dev_name = str(dev.get('DeviceName'))
# if dev.get('Ebs', None) is None:
# continue
# vol_id = dev['Ebs']['VolumeId']
# print "Found EBS volume %s named %s on instance %s" % (
# vol_id, dev_name, instance['InstanceId'])
# snap = ec.create_snapshot(
# VolumeId=vol_id, Description="Weekly Backup - View DeviceName and DeleteOn Tags - " + desc
# )
create_time = datetime.datetime.now()
create_fmt = create_time.strftime('%Y-%m-%d')
AMIid = ec.create_image(InstanceId=instance['InstanceId'], Name="AMI - " + name + " from " + create_fmt, Description="AMI Backup of instance " + instance['InstanceId'] + " from " + create_fmt, NoReboot=True, DryRun=False)
#pprint.pprint(instance)
to_tag[retention_days].append(AMIid['ImageId'])
print "Retaining AMI %s of instance %s for %d days" % (
AMIid['ImageId'],
instance['InstanceId'],
retention_days,
)
print to_tag.keys()
for retention_days in to_tag.keys():
delete_date = datetime.date.today() + datetime.timedelta(days=retention_days)
delete_fmt = delete_date.strftime('%Y-%m-%d')
print "Will delete %d AMIs on %s" % (len(to_tag[retention_days]), delete_fmt)
ec.create_tags(
Resources=to_tag[retention_days],
Tags=[
{'Key': 'DeleteOn', 'Value': delete_fmt},
]
)
# Check if instance is in an ASG already
try:
instanceASG = asg.describe_auto_scaling_instances(InstanceIds=[instance['InstanceId']])['AutoScalingInstances'][0]['AutoScalingGroupName']
# Get old launch configuration (to be deleted)
oldLC = asg.describe_auto_scaling_groups(AutoScalingGroupNames=[instanceASG])['AutoScalingGroups'][0]['LaunchConfigurationName']
# create LC using instance from target ASG as a template, only diff is the name of the new LC and new AMI
timeStamp = time.time()
timeStampString = datetime.datetime.fromtimestamp(timeStamp).strftime('%Y-%m-%d')
newLaunchConfigName = name + '-LC ' + AMIid['ImageId'] + ' ' + timeStampString
asg.create_launch_configuration(
InstanceId = instance['InstanceId'],
LaunchConfigurationName = newLaunchConfigName,
ImageId= AMIid['ImageId'] )
# update ASG to use new LC
asg.update_auto_scaling_group(AutoScalingGroupName = instanceASG,LaunchConfigurationName = newLaunchConfigName)
# delete old LC
asg.delete_launch_configuration(LaunchConfigurationName = oldLC)
print 'Updated ASG `%s` with new launch configuration `%s` which includes AMI `%s`.' % (instanceASG, newLaunchConfigName, AMIid['ImageId'])
except:
print 'No ASG for ' + name + ', continuing backup'