-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
137 lines (113 loc) · 4.29 KB
/
main.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
import boto3
import logging
import os
import sys
import datetime
# Init logger
logger = logging.getLogger(__name__)
# Set log level
if os.environ.get('LOG_LEVEL') is None:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.os.environ['LOG_LEVEL'])
# Set region
if os.environ.get('REGION') is None:
region = 'eu-central-1'
else:
region = os.environ['REGION']
# session = boto3.Session(profile_name='mm', region_name=region)
session = boto3.Session(region_name=region)
ec2 = session.resource('ec2')
today = datetime.date.today().strftime("%A")
def create_ec2_volume_snapshot(volume_name, volume_id, instance_id, instance_name):
logger.info("Creating snapshot for volume {} with id of {} with day tag of {}".format(volume_name, volume_id, today))
ec2.create_snapshot(
Description="Snapshot for {} with id {} of ec2 instance {} with id".format(volume_name, volume_id,instance_name ,instance_id),
VolumeId=volume_id,
TagSpecifications=[
{
'ResourceType': 'snapshot',
'Tags': [
{
'Key': 'DeleteOn',
'Value': today
},
{
'Key': 'Name',
'Value': volume_name + '_' + instance_name + '_' + instance_id + '_' + today
},
{
'Key': 'volume_id',
'Value': volume_id
},
{
'Key': 'Originator',
'Value': 'Lambda'
},
]
},
]
)
def get_ec2_instance_for_backups():
instances = ec2.instances.filter(Filters=[{'Name': 'tag:Backup', 'Values': ['Yes']}])
if instances is None:
return None
else:
return instances
def get_ec2_instance_volumes(instance):
for volume in instance.block_device_mappings:
logger.debug("volume ids: {}".format(volume['Ebs']['VolumeId']))
logger.debug("volume ids: {}".format(volume['DeviceName']))
logger.debug("Instance name is {}".format(instance.meta.data['KeyName']))
instance_name = instance.meta.data['KeyName']
if instance_name is None:
instance_name = 'No_Name_Tag'
if volume is not None:
create_ec2_volume_snapshot(volume['DeviceName'], volume['Ebs']['VolumeId'], instance.id, instance_name)
def list_all_volumes_to_delete():
filter = [
{'Name': 'tag:DeleteOn', 'Values': [today]}
]
client = session.client('ec2')
snapshots_to_delete = client.describe_snapshots(Filters=filter)
if snapshots_to_delete is not None:
return snapshots_to_delete
else:
return None
def delete_snapshot(snapshot_id):
logger.debug("Deleting snapthot {}".format(snapshot_id))
try:
client = session.client('ec2')
response = client.delete_snapshot(
SnapshotId=snapshot_id,
DryRun=False,
)
logger.info("deleted snapthot {}".format(str(response)))
except Exception as ex:
logger.fatal("Failed to delete snapshot {} with the exception of {}".format(snapshot_id, str(ex)))
def lambda_handler(event, context):
logger.info("Checking deleting old snapshots")
volumes_list = list_all_volumes_to_delete()
if volumes_list is not None:
for volume in volumes_list['Snapshots']:
logger.debug(volume)
delete_snapshot(volume['SnapshotId'])
else:
logger.info("There are no snapshot to delete")
logger.info("Listing all EC2 instances that needs backups")
ec2_instances = get_ec2_instance_for_backups()
if ec2_instances is None:
logger.info("No backups needed")
else:
for ec2_instance in ec2_instances:
logger.debug("Setting backups for {}".format(ec2_instance.id))
logger.info("Getting list of Volumes for instance {}".format(ec2_instance.id))
get_ec2_instance_volumes(ec2_instance)
if __name__ == "__main__":
event = ''
context = []
# Logger
channel = logging.StreamHandler(sys.stdout)
channel.setFormatter(logging.Formatter('%(asctime)s\t%(name)s\t%(levelname)s\t%(message)s'))
logger.addHandler(channel)
lambda_handler(event, context)