-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrun_create_rds.py
executable file
·167 lines (137 loc) · 5.55 KB
/
run_create_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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
import datetime
import json
import re
import time
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 create_iam_for_rds():
sleep_required = False
rr = aws_cli.get_iam_user()
user_name = rr['User']['UserName']
ua = re.match(r'^arn:aws:iam::([0-9]+):user.+$', rr['User']['Arn'])
policy_name = 'aws-rds-monitoring-role-allow-policy'
if not aws_cli.get_iam_user_policy(user_name, policy_name):
rds_policy = {
'Version': '2012-10-17',
'Statement': [
{
'Effect': 'Allow',
'Action': [
'iam:PassRole'
],
'Resource': f'arn:aws:iam::{ua[1]}:role/rds-monitoring-role'
}
]
}
cc = ['iam', 'put-user-policy']
cc += ['--user-name', user_name]
cc += ['--policy-name', policy_name]
cc += ['--policy-document', json.dumps(rds_policy)]
aws_cli.run(cc)
sleep_required = True
role_name = 'rds-monitoring-role'
if not aws_cli.get_iam_role(role_name):
print_message('create iam role')
cc = ['iam', 'create-role']
cc += ['--role-name', role_name]
cc += ['--assume-role-policy-document', 'file://aws_iam/rds-monitoring-role.json']
aws_cli.run(cc)
cc = ['iam', 'attach-role-policy']
cc += ['--role-name', role_name]
cc += ['--policy-arn', 'arn:aws:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole']
aws_cli.run(cc)
sleep_required = True
if sleep_required:
print_message('wait 120 seconds to let iam role and policy propagated to all regions...')
time.sleep(120)
db_backup_retention_period = env['rds']['BACKUP_RETENTION_PERIOD']
db_instance_class = env['rds']['DB_CLASS']
db_instance_id = env['rds']['DB_INSTANCE_ID']
db_iops = env['rds']['IOPS']
db_multi_az = env['rds']['MULTI_AZ']
db_subnet_group_name = env['rds']['DB_SUBNET_NAME']
license_model = env['rds']['LICENSE_MODEL']
logs_export_to_cloudwatch = json.dumps(['error', 'general', 'audit', 'slowquery'])
master_user_name = env['rds']['USER_NAME']
master_user_password = env['rds']['USER_PASSWORD']
monitoring_interval = env['rds']['MONITORING_INTERVAL']
cidr_subnet = aws_cli.cidr_subnet
################################################################################
#
# start
#
################################################################################
print_session('create rds')
create_iam_for_rds()
################################################################################
print_message('get vpc id')
rds_vpc_id, eb_vpc_id = aws_cli.get_vpc_id()
if not rds_vpc_id or not eb_vpc_id:
print('ERROR!!! No VPC found')
raise Exception()
################################################################################
print_message('get security group id')
security_group_id = None
cmd = ['ec2', 'describe-security-groups']
result = aws_cli.run(cmd)
for r in result['SecurityGroups']:
if r['VpcId'] != rds_vpc_id:
continue
if r['GroupName'] == 'default':
continue
if not security_group_id:
security_group_id = r['GroupId']
else:
raise Exception()
################################################################################
print_message('get rds role arn')
monitoring_role_arn = aws_cli.get_role_arn('rds-monitoring-role')
################################################################################
print_message('create rds')
cmd = ['rds', 'create-db-cluster']
cmd += ['--backup-retention-period', db_backup_retention_period]
cmd += ['--db-cluster-identifier', env['rds']['DB_CLUSTER_ID']]
cmd += ['--db-subnet-group-name', db_subnet_group_name]
cmd += ['--enable-cloudwatch-logs-exports', logs_export_to_cloudwatch]
cmd += ['--engine', 'aurora-mysql']
cmd += ['--engine-version', '8.0.mysql_aurora.3.05.2']
cmd += ['--master-user-password', master_user_password]
cmd += ['--master-username', master_user_name]
cmd += ['--vpc-security-group-ids', security_group_id]
aws_cli.run(cmd)
aws_cli.wait_create_rds_cluster(env['rds']['DB_CLUSTER_ID'])
cmd = ['rds', 'create-db-instance']
cmd += ['--db-cluster-identifier', env['rds']['DB_CLUSTER_ID']]
cmd += ['--db-instance-class', db_instance_class]
cmd += ['--db-instance-identifier', db_instance_id]
cmd += ['--engine', 'aurora-mysql']
cmd += ['--iops', db_iops]
cmd += ['--license-model', license_model]
cmd += ['--monitoring-interval', monitoring_interval]
cmd += ['--monitoring-role-arn', monitoring_role_arn]
if not db_instance_class.startswith('db.t'):
cmd += ['--enable-performance-insights']
cmd += ['--ca-certificate-identifier', 'rds-ca-rsa2048-g1']
aws_cli.run(cmd)
if db_multi_az == '--multi-az':
cmd = ['rds', 'create-db-instance']
cmd += ['--db-cluster-identifier', env['rds']['DB_CLUSTER_ID']]
cmd += ['--db-instance-class', db_instance_class]
ss = datetime.datetime.today().strftime('%Y%m%d')
cmd += ['--db-instance-identifier', '%s-%s' % (db_instance_id, ss)]
cmd += ['--engine', 'aurora-mysql']
cmd += ['--iops', db_iops]
cmd += ['--license-model', license_model]
cmd += ['--monitoring-interval', monitoring_interval]
cmd += ['--monitoring-role-arn', monitoring_role_arn]
if not db_instance_class.startswith('db.t'):
cmd += ['--enable-performance-insights']
cmd += ['--ca-certificate-identifier', 'rds-ca-rsa2048-g1']
aws_cli.run(cmd)