-
Notifications
You must be signed in to change notification settings - Fork 16
/
run_create_route53.py
executable file
·145 lines (114 loc) · 4.16 KB
/
run_create_route53.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
#!/usr/bin/env python3.12
import json
from argparse import ArgumentParser
from run_common import AWSCli
from run_common import _confirm_phase
from run_common import print_session
################################################################################
#
# start
#
################################################################################
def parse_args():
parser = ArgumentParser()
parser.add_argument('-f', '--force', action='store_true', help='pass confirm')
parser.add_argument('-ah', '--alias_hosted_zone_id', type=str, required=False, help='hosted zone id of alias')
parser.add_argument('-at', '--alias_type', type=str, required=False, choices=['cloudfront', 's3'],
help='type of alias')
parser.add_argument('-d', '--domain_name', type=str, required=True,
help='domain name(s3 -> end point, cloudfront -> domain name)')
parser.add_argument('-hn', '--hosted_zone_name', type=str, required=True, help='name of hosted zone')
parser.add_argument('-n', '--name', type=str, required=True, help='acm arn')
parser.add_argument('-r', '--record_set_type', type=str, required=True, help='type of record_set')
args = parser.parse_args()
if not args.force:
_confirm_phase()
return args
def is_exist_record_set(host_zone_name, name):
print_session('is_exist_record_set')
aws_cli = AWSCli()
cmd = ['route53', 'list-hosted-zones-by-name']
cmd += ['--dns-name', host_zone_name]
rr = aws_cli.run(cmd)
if not (len(rr['HostedZones']) == 1):
raise Exception('wrong host zone')
hosted_zone_id = rr['HostedZones'][0]['Id']
cmd = ['route53', 'list-resource-record-sets']
cmd += ['--hosted-zone-id', hosted_zone_id]
rr = aws_cli.run(cmd)
for vv in rr['ResourceRecordSets']:
if vv['Name'] == name:
print_session('exist record set(%s)' % name)
return True
return False
def find_host_zone_id(host_zone_name):
print_session('find_host_zone_id')
aws_cli = AWSCli()
cmd = ['route53', 'list-hosted-zones-by-name']
cmd += ['--dns-name', host_zone_name]
rr = aws_cli.run(cmd)
if not (len(rr['HostedZones']) == 1):
raise Exception('wrong host zone')
return rr['HostedZones'][0]['Id']
def find_cloudfront(domain_name):
aws_cli = AWSCli()
cmd = ['cloudfront', 'list-distributions']
rr = aws_cli.run(cmd)
for vv in rr['DistributionList']['Items']:
if 'Items' in vv['Aliases'] and domain_name in vv['Aliases']['Items']:
return vv
def create_route53(name, host_zone_name, type, domain_name, alias=None):
rrs = dict()
if alias:
if alias['TYPE'] == 'cloudfront':
rr = find_cloudfront(domain_name)
domain_name = rr['DomainName']
rrs = {
"Name": name,
"Type": type,
"AliasTarget": {
"HostedZoneId": alias['HOSTED_ZONE_ID'],
"DNSName": domain_name,
"EvaluateTargetHealth": False
}
}
else:
rrs = {
"Name": name,
"Type": type,
"ResourceRecord": [
{
"Value": domain_name
}
]
}
dd = dict()
dd['Changes'] = [
{
"Action": "CREATE",
"ResourceRecordSet": rrs
}
]
aws_cli = AWSCli()
cmd = ['route53', 'change-resource-record-sets']
id = find_host_zone_id(host_zone_name)
cmd += ['--hosted-zone-id', id]
cmd += ['--change-batch', json.dumps(dd)]
rr = aws_cli.run(cmd)
return rr
if __name__ == '__main__':
args = parse_args()
hosted_zone_name = args.hosted_zone_name
name = args.name
record_set_type = args.record_set_type
domain_name = args.domain_name
alias_hosted_zone_id = args.alias_hosted_zone_id
alias_type = args.alias_type
alias = None
if alias_type:
alias = dict()
alias['TYPE'] = alias_type
alias['HOSTED_ZONE_ID'] = alias_hosted_zone_id
if is_exist_record_set(hosted_zone_name, name):
exit(1)
create_route53(name, hosted_zone_name, record_set_type, domain_name, alias)