-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebs_delete_on_term.py
executable file
·68 lines (55 loc) · 2.1 KB
/
ebs_delete_on_term.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
#! /usr/local/bin/python
import boto3
import argparse
import json
def indent(t, *args):
print '\t' * t + ' '.join([str(s) for s in args])
def get_instance_name(instance):
for t in instance['Tags']:
if t['Key'] == 'Name':
return t['Value']
return 'Has no name tag'
def get_instance_block_device(instance, device_name):
for device in i['BlockDeviceMappings']:
if device['DeviceName'] == device_name:
device = {
'name': device['DeviceName'],
'delete_on_termination': device['Ebs']['DeleteOnTermination'],
'id': device['Ebs']['VolumeId']
}
return device
parser = argparse.ArgumentParser(
description='Set EBS block device delete_on_termination attribute on multiple '
'instances chosen by instance tags.')
parser.add_argument('--device', type=str, required=False, help='/dev/xvdb')
parser.add_argument('--instance-tags', type=json.loads, help='{"env":["production"], ...}')
parser.add_argument('--dry-run', action='store_true')
parser.add_argument('--region', type=str)
commands = parser.add_mutually_exclusive_group(required=True)
commands.add_argument('--enable', action='store_true')
commands.add_argument('--disable', action='store_true')
args = parser.parse_args()
ec2 = boto3.client('ec2', region_name=args.region)
print 'connected to region', ec2._client_config.region_name
instances = [
i["Instances"][0] for i in ec2.describe_instances(
Filters=[{'Name': 'tag:' + k, 'Values': v}
for k, v in args.instance_tags.items()]
)['Reservations']
]
for i in instances:
d = get_instance_block_device(i, args.device)
if not d:
continue
print get_instance_name(i)
indent(1, d['id'], 'Old setting:', d['delete_on_termination'])
if not args.dry_run:
ec2.modify_instance_attribute(
InstanceId=i['InstanceId'],
BlockDeviceMappings=[
{
'DeviceName': args.device,
'Ebs': {'DeleteOnTermination': args.enable or not args.disable}
}
]
)