|
2 | 2 | import collections
|
3 | 3 | import datetime
|
4 | 4 |
|
5 |
| -ec = boto3.client('ec2') |
| 5 | +regions = ['us-west-2','us-east-2'] |
6 | 6 |
|
7 | 7 | def lambda_handler(event, context):
|
8 |
| - reservations = ec.describe_instances( |
9 |
| - Filters=[ |
10 |
| - {'Name': 'tag-key', 'Values': ['backup', 'Backup']}, |
11 |
| - ] |
12 |
| - ).get( |
13 |
| - 'Reservations', [] |
14 |
| - ) |
| 8 | + for region in regions: |
| 9 | + ec = boto3.client('ec2', region_name=region) |
| 10 | + reservations = ec.describe_instances( |
| 11 | + Filters=[ |
| 12 | + {'Name': 'tag-key', 'Values': ['backup', 'Backup']}, |
| 13 | + ] |
| 14 | + ).get( |
| 15 | + 'Reservations', [] |
| 16 | + ) |
15 | 17 |
|
16 |
| - instances = sum( |
17 |
| - [ |
18 |
| - [i for i in r['Instances']] |
19 |
| - for r in reservations |
20 |
| - ], []) |
| 18 | + instances = sum( |
| 19 | + [ |
| 20 | + [i for i in r['Instances']] |
| 21 | + for r in reservations |
| 22 | + ], []) |
21 | 23 |
|
22 |
| - print "Found %d instances that need backing up" % len(instances) |
| 24 | + print "Found %d instances that need backing up" % len(instances) |
23 | 25 |
|
24 |
| - to_tag_retention = collections.defaultdict(list) |
25 |
| - to_tag_mount_point = collections.defaultdict(list) |
| 26 | + to_tag_retention = collections.defaultdict(list) |
| 27 | + to_tag_mount_point = collections.defaultdict(list) |
26 | 28 |
|
27 |
| - for instance in instances: |
28 |
| - try: |
29 |
| - retention_days = [ |
30 |
| - int(t.get('Value')) for t in instance['Tags'] |
31 |
| - if t['Key'] == 'Retention'][0] |
32 |
| - except IndexError: |
33 |
| - retention_days = 7 |
| 29 | + for instance in instances: |
| 30 | + try: |
| 31 | + retention_days = [ |
| 32 | + int(t.get('Value')) for t in instance['Tags'] |
| 33 | + if t['Key'] == 'Retention'][0] |
| 34 | + except IndexError: |
| 35 | + retention_days = 7 |
34 | 36 |
|
35 |
| - for dev in instance['BlockDeviceMappings']: |
36 |
| - if dev.get('Ebs', None) is None: |
37 |
| - continue |
38 |
| - vol_id = dev['Ebs']['VolumeId'] |
39 |
| - dev_attachment = dev['DeviceName'] |
40 |
| - print "Found EBS volume %s on instance %s attached to %s" % ( |
41 |
| - vol_id, instance['InstanceId'], dev_attachment) |
| 37 | + for dev in instance['BlockDeviceMappings']: |
| 38 | + if dev.get('Ebs', None) is None: |
| 39 | + continue |
| 40 | + vol_id = dev['Ebs']['VolumeId'] |
| 41 | + dev_attachment = dev['DeviceName'] |
| 42 | + print "Found EBS volume %s on instance %s attached to %s" % ( |
| 43 | + vol_id, instance['InstanceId'], dev_attachment) |
42 | 44 |
|
43 |
| - snap = ec.create_snapshot( |
44 |
| - VolumeId=vol_id, |
45 |
| - Description=instance['InstanceId'], |
46 |
| - ) |
| 45 | + snap = ec.create_snapshot( |
| 46 | + VolumeId=vol_id, |
| 47 | + Description=instance['InstanceId'], |
| 48 | + ) |
47 | 49 |
|
48 |
| - to_tag_retention[retention_days].append(snap['SnapshotId']) |
49 |
| - to_tag_mount_point[vol_id].append(snap['SnapshotId']) |
| 50 | + to_tag_retention[retention_days].append(snap['SnapshotId']) |
| 51 | + to_tag_mount_point[vol_id].append(snap['SnapshotId']) |
50 | 52 |
|
51 | 53 |
|
52 |
| - print "Retaining snapshot %s of volume %s from instance %s for %d days" % ( |
53 |
| - snap['SnapshotId'], |
54 |
| - vol_id, |
55 |
| - instance['InstanceId'], |
56 |
| - retention_days, |
57 |
| - ) |
| 54 | + print "Retaining snapshot %s of volume %s from instance %s for %d days" % ( |
| 55 | + snap['SnapshotId'], |
| 56 | + vol_id, |
| 57 | + instance['InstanceId'], |
| 58 | + retention_days, |
| 59 | + ) |
58 | 60 |
|
| 61 | + ec.create_tags( |
| 62 | + Resources=to_tag_mount_point[vol_id], |
| 63 | + Tags=[ |
| 64 | + {'Key': 'Name', 'Value': dev_attachment}, |
| 65 | + ] |
| 66 | + ) |
| 67 | + |
| 68 | + for retention_days in to_tag_retention.keys(): |
| 69 | + delete_date = datetime.date.today() + datetime.timedelta(days=retention_days) |
| 70 | + delete_fmt = delete_date.strftime('%Y-%m-%d') |
| 71 | + print "Will delete %d snapshots on %s" % (len(to_tag_retention[retention_days]), delete_fmt) |
59 | 72 | ec.create_tags(
|
60 |
| - Resources=to_tag_mount_point[vol_id], |
| 73 | + Resources=to_tag_retention[retention_days], |
61 | 74 | Tags=[
|
62 |
| - {'Key': 'Name', 'Value': dev_attachment}, |
| 75 | + {'Key': 'DeleteOn', 'Value': delete_fmt}, |
63 | 76 | ]
|
64 | 77 | )
|
65 |
| - |
66 |
| - for retention_days in to_tag_retention.keys(): |
67 |
| - delete_date = datetime.date.today() + datetime.timedelta(days=retention_days) |
68 |
| - delete_fmt = delete_date.strftime('%Y-%m-%d') |
69 |
| - print "Will delete %d snapshots on %s" % (len(to_tag_retention[retention_days]), delete_fmt) |
70 |
| - ec.create_tags( |
71 |
| - Resources=to_tag_retention[retention_days], |
72 |
| - Tags=[ |
73 |
| - {'Key': 'DeleteOn', 'Value': delete_fmt}, |
74 |
| - ] |
75 |
| - ) |
|
0 commit comments