-
Notifications
You must be signed in to change notification settings - Fork 16
/
run_terminate_rds.py
executable file
·68 lines (52 loc) · 2.04 KB
/
run_terminate_rds.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/bin/env python3
from env import env
from run_common import AWSCli
from run_common import print_message
from run_common import print_session
if __name__ == "__main__":
from run_common import parse_args
parse_args()
aws_cli = AWSCli()
def terminate_iam_for_rds():
print_message('delete iam role')
# noinspection PyShadowingNames
cc = ['iam', 'detach-role-policy']
cc += ['--role-name', 'rds-monitoring-role']
cc += ['--policy-arn', 'arn:aws:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole']
aws_cli.run(cc, ignore_error=True)
# noinspection PyShadowingNames
cc = ['iam', 'delete-role']
cc += ['--role-name', 'rds-monitoring-role']
aws_cli.run(cc, ignore_error=True)
rr = aws_cli.get_iam_user()
user_name = rr['User']['UserName']
# noinspection PyShadowingNames
cc = ['iam', 'delete-user-policy']
cc += ['--user-name', user_name]
cc += ['--policy-name', 'aws-rds-monitoring-role-allow-policy']
aws_cli.run(cc, ignore_error=True)
################################################################################
#
# Start
#
################################################################################
print_session('terminate rds')
################################################################################
print_message('delete rds')
cmd = ['rds', 'describe-db-clusters']
cmd += ['--db-cluster-identifier', env['rds']['DB_CLUSTER_ID']]
result = aws_cli.run(cmd, ignore_error=True)
if isinstance(result, dict):
cluster_list = result.get('DBClusters', list())
for cc in cluster_list:
member_list = cc['DBClusterMembers']
for mm in member_list:
cmd = ['rds', 'delete-db-instance']
cmd += ['--db-instance-identifier', mm['DBInstanceIdentifier']]
cmd += ['--skip-final-snapshot']
aws_cli.run(cmd, ignore_error=True)
cmd = ['rds', 'delete-db-cluster']
cmd += ['--db-cluster-identifier', env['rds']['DB_CLUSTER_ID']]
cmd += ['--skip-final-snapshot']
aws_cli.run(cmd, ignore_error=True)
terminate_iam_for_rds()