11import boto3
2- import collections
3- import datetime
4- import os
5-
6- ec = boto3 .client ('ec2' )
7-
8- def lambda_handler (event , context ):
9-
10- # Get Current Region
11- aws_region = os .getenv ('AWS_REGION' )
12-
13- # Determine Which Instances To SnapShot
14- instances = ec .describe_instances (
15- Filters = [
16- { 'Name' : 'tag:Backup' , 'Values' : ['Yes' ] },
17- ]
18- ).get (
19- 'Reservations' , []
20- )[0 ]['Instances' ]
21-
22- print "Found %d instances that need backing up" % len (instances )
23-
24- # Iterate Over Each Instance & SnapShot Volumes Not Explicitly Excluded From Backups
25- for instance in instances :
26-
27- # Determine Retention Period Based Upon Tags
28- retention_days = 7
29- destination_region = None
30- instance_name = ""
31- for tag in instance ['Tags' ]:
32- if tag ['Key' ] == 'RetentionDays' and tag ['Value' ] > 0 :
33- retention_days = int (tag ['Value' ])
34-
35- if tag ['Key' ] == 'DestinationRegion' and len (tag ['Value' ]) > 0 :
36- destination_region = tag ['Value' ]
37-
38- if tag ['Key' ] == 'Name' and len (tag ['Value' ]) > 0 :
39- instance_name = tag ['Value' ]
40-
41- print "Setting SnapShot retention period To %s days" % (retention_days )
42-
43- # Determine When We're Going To Delete This SnapShot
44- delete_date = datetime .date .today () + datetime .timedelta (days = retention_days )
45- delete_fmt = delete_date .strftime ('%Y-%m-%d' )
46-
47- # Set Default SnapShot Tags
48- snapshot_tags = [
49- { 'Key' : 'DeleteOn' , 'Value' : delete_fmt },
50- { 'Key' : 'Type' , 'Value' : 'Automated' },
51- ]
52-
53- # If We Want To Offsite This SnapShot, Set The Appropriate Tag
54- if destination_region != None :
55- snapshot_tags = snapshot_tags + [{ 'Key' : 'DestinationRegion' , 'Value' : destination_region }]
56-
57- # List All Volumes Attached To The Instance
58- for dev in instance ['BlockDeviceMappings' ]:
59-
60- # Set Variable Defaults
61- snapshot_required = True
62- volume_name = None
63-
64- if dev .get ('Ebs' , None ) is None :
65- continue
66- vol_id = dev ['Ebs' ]['VolumeId' ]
67- dev_name = dev ['DeviceName' ]
68-
69- # Get a Volume Object Based Upon Volume ID
70- volume = ec .describe_volumes (
71- VolumeIds = [vol_id ,]
72- )['Volumes' ][0 ]
73-
74- # Set Default SnapShot Description
75- description = '%s - %s (%s)' % (
76- instance_name ,
77- vol_id ,
78- dev_name
79- )
80-
81- if 'Tags' in volume :
82- for tag in volume ['Tags' ]:
83-
84- # Determine If Volume Has 'Backup' Flag Set To 'No' & Exclude From SnapShot If It Does
85- if tag ['Key' ] == 'Backup' and tag ['Value' ] == 'No' :
86- snapshot_required = False
87-
88- # Override Default Description With Volume Name If One Specified
89- if tag ['Key' ] == 'Name' :
90- description = tag ['Value' ]
91-
92-
93- # We Don't Want To SnapShot Any Volume Explictly Excluded
94- if snapshot_required == False :
95- print "\t Ignoring EBS volume %s (%s) on instance %s - 'Backup' Tag set to 'No'" % (
96- vol_id ,
97- dev_name ,
98- instance ['InstanceId' ]
99- )
100-
101- continue
102-
103-
104- print "\t Found EBS volume %s (%s) on instance %s - Proceeding with SnapShot" % (
105- vol_id ,
106- dev_name ,
107- instance ['InstanceId' ]
108- )
109-
110- # Take SnapShot Of Volume
111- snap = ec .create_snapshot (
112- VolumeId = vol_id ,
113- Description = description
114- )
115-
116- if not (snap ):
117- print "\t \t SnapShot operation failed!"
118- continue
119-
120- print "\t \t Snapshot %s created in %s of [%s]" % (
121- snap ['SnapshotId' ],
122- aws_region ,
123- description
124- )
125-
126- print "\t \t Retaining snapshot %s of volume %s from instance %s (%s) for %d days" % (
127- snap ['SnapshotId' ],
128- vol_id ,
129- instance ['InstanceId' ],
130- instance_name ,
131- retention_days ,
132- )
133-
134- # Tag The SnapShot To Facilitate Later Automated Deletion & Offsiting
135- ec .create_tags (
136- Resources = [snap ['SnapshotId' ]],
137- Tags = snapshot_tags
138- )
2+ import collections
3+ import datetime
4+ import os
5+
6+ ec = boto3 .client ('ec2' )
7+
8+ def lambda_handler (event , context ):
9+
10+ # Get Current Region
11+ aws_region = os .getenv ('AWS_REGION' )
12+
13+ # Determine Which Instances To SnapShot
14+ instances = ec .describe_instances (
15+ Filters = [
16+ { 'Name' : 'tag:Backup' , 'Values' : ['Yes' ] },
17+ ]
18+ ).get (
19+ 'Reservations' , []
20+ )[0 ]['Instances' ]
21+
22+ print "Found %d instances that need backing up" % len (instances )
23+
24+ # Iterate Over Each Instance & SnapShot Volumes Not Explicitly Excluded From Backups
25+ for instance in instances :
26+
27+ # Determine Retention Period Based Upon Tags
28+ retention_days = 7
29+ destination_region = None
30+ instance_name = ""
31+ for tag in instance ['Tags' ]:
32+ if tag ['Key' ] == 'RetentionDays' and tag ['Value' ] > 0 :
33+ retention_days = int (tag ['Value' ])
34+
35+ if tag ['Key' ] == 'DestinationRegion' and len (tag ['Value' ]) > 0 :
36+ destination_region = tag ['Value' ]
37+
38+ if tag ['Key' ] == 'Name' and len (tag ['Value' ]) > 0 :
39+ instance_name = tag ['Value' ]
40+
41+ print "Setting SnapShot retention period To %s days" % (retention_days )
42+
43+ # Determine When We're Going To Delete This SnapShot
44+ delete_date = datetime .date .today () + datetime .timedelta (days = retention_days )
45+ delete_fmt = delete_date .strftime ('%Y-%m-%d' )
46+
47+ # Set Default SnapShot Tags
48+ snapshot_tags = [
49+ { 'Key' : 'DeleteOn' , 'Value' : delete_fmt },
50+ { 'Key' : 'Type' , 'Value' : 'Automated' },
51+ ]
52+
53+ # If We Want To Offsite This SnapShot, Set The Appropriate Tag
54+ if destination_region != None :
55+ snapshot_tags = snapshot_tags + [{ 'Key' : 'DestinationRegion' , 'Value' : destination_region }]
56+
57+ # List All Volumes Attached To The Instance
58+ for dev in instance ['BlockDeviceMappings' ]:
59+
60+ # Set Variable Defaults
61+ snapshot_required = True
62+ volume_name = None
63+
64+ if dev .get ('Ebs' , None ) is None :
65+ continue
66+ vol_id = dev ['Ebs' ]['VolumeId' ]
67+ dev_name = dev ['DeviceName' ]
68+
69+ # Get a Volume Object Based Upon Volume ID
70+ volume = ec .describe_volumes (
71+ VolumeIds = [vol_id ,]
72+ )['Volumes' ][0 ]
73+
74+ # Set Default SnapShot Description
75+ description = '%s - %s (%s)' % (
76+ instance_name ,
77+ vol_id ,
78+ dev_name
79+ )
80+
81+ if 'Tags' in volume :
82+ for tag in volume ['Tags' ]:
83+
84+ # Determine If Volume Has 'Backup' Flag Set To 'No' & Exclude From SnapShot If It Does
85+ if tag ['Key' ] == 'Backup' and tag ['Value' ] == 'No' :
86+ snapshot_required = False
87+
88+ # Override Default Description With Volume Name If One Specified
89+ if tag ['Key' ] == 'Name' :
90+ description = tag ['Value' ]
91+
92+
93+ # We Don't Want To SnapShot Any Volume Explictly Excluded
94+ if snapshot_required == False :
95+ print "\t Ignoring EBS volume %s (%s) on instance %s - 'Backup' Tag set to 'No'" % (
96+ vol_id ,
97+ dev_name ,
98+ instance ['InstanceId' ]
99+ )
100+
101+ continue
102+
103+
104+ print "\t Found EBS volume %s (%s) on instance %s - Proceeding with SnapShot" % (
105+ vol_id ,
106+ dev_name ,
107+ instance ['InstanceId' ]
108+ )
109+
110+ # Take SnapShot Of Volume
111+ snap = ec .create_snapshot (
112+ VolumeId = vol_id ,
113+ Description = description
114+ )
115+
116+ if not (snap ):
117+ print "\t \t SnapShot operation failed!"
118+ continue
119+
120+ print "\t \t Snapshot %s created in %s of [%s]" % (
121+ snap ['SnapshotId' ],
122+ aws_region ,
123+ description
124+ )
125+
126+ print "\t \t Retaining snapshot %s of volume %s from instance %s (%s) for %d days" % (
127+ snap ['SnapshotId' ],
128+ vol_id ,
129+ instance ['InstanceId' ],
130+ instance_name ,
131+ retention_days ,
132+ )
133+
134+ # Tag The SnapShot To Facilitate Later Automated Deletion & Offsiting
135+ ec .create_tags (
136+ Resources = [snap ['SnapshotId' ]],
137+ Tags = snapshot_tags
138+ )
0 commit comments