-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-route53-zone.py
executable file
·110 lines (98 loc) · 4.2 KB
/
get-route53-zone.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
#!/usr/local/bin/python3
import sys
import route53
def a_record(record_set):
print("resource \"aws_route53_record\" \"" + record_set.name.replace(".", "_")[:-1] + "\" {")
print(" zone_id = aws_route53_zone." + zone.name.replace(".", "_")[:-1] + ".zone_id")
print(" name = \"" + record_set.name[:-1] + "\"")
print(" type = \"A\"")
print(" ttl = \"300\"")
if len(record_set.records) == 1:
print(" records = [\"" + ''.join(record_set.records) + "\"]")
else:
print(" records = [\"" + '\", \"'.join(record_set.records) + "\"]")
print("}")
print("")
def txt_record(record_set):
print("resource \"aws_route53_record\" \"" + record_set.name.replace(".", "_")[:-1] + "\" {")
print(" zone_id = aws_route53_zone." + zone.name.replace(".", "_")[:-1] + ".zone_id")
print(" name = \"" + record_set.name[:-1] + "\"")
print(" type = \"TXT\"")
print(" ttl = \"300\"")
print(" records = [" + ''.join(record_set.records) + "]")
print("}")
print("")
def mx_record(record_set):
print("resource \"aws_route53_record\" \"mx_" + record_set.name.replace(".", "_")[:-1] + "\" {")
print(" zone_id = aws_route53_zone." + zone.name.replace(".", "_")[:-1] + ".zone_id")
print(" name = \"" + record_set.name[:-1] + "\"")
print(" type = \"MX\"")
print(" ttl = \"300\"")
print(" records = [\"" + ''.join(record_set.records) + "\"]")
print("}")
print("")
def alias_record(record_set):
print("resource \"aws_route53_record\" \"" + record_set.name.replace(".", "_")[:-1] + "\" {")
print(" zone_id = aws_route53_zone." + zone.name.replace(".", "_")[:-1] + ".zone_id")
print(" name = \"" + record_set.name[:-1] + "\"")
print(" type = \"A\"")
print(" alias {")
print(" name = \"" + record_set.alias_dns_name + "\"")
print(" zone_id = \"" + record_set.alias_hosted_zone_id + "\"")
print(" evaluate_target_health = false")
print(" }")
print("}")
print("")
def cname_record(record_set):
print("resource \"aws_route53_record\" \"" + record_set.name.replace(".", "_")[:-1] + "\" {")
print(" zone_id = aws_route53_zone." + zone.name.replace(".", "_")[:-1] + ".zone_id")
print(" name = \"" + record_set.name[:-1] + "\"")
print(" type = \"CNAME\"")
print(" ttl = \"300\"")
print(" records = [\"" + ''.join(record_set.records) + "\"]")
print("}")
print("")
def ns_record(record_set):
print("resource \"aws_route53_record\" \"" + record_set.name.replace(".", "_")[:-1] + "\" {")
print(" zone_id = aws_route53_zone." + zone.name.replace(".", "_")[:-1] + ".zone_id")
print(" name = \"" + record_set.name[:-1] + "\"")
print(" type = \"NS\"")
print(" ttl = \"300\"")
if len(record_set.records) == 1:
print(" records = [" + ''.join(record_set.records) + "]")
else:
print(" records = [\"" + '\", \"'.join(record_set.records) + "\"]")
print("}")
print("")
if __name__ == "__main__":
if len(sys.argv) == 1:
print("Usage: " + sys.argv[0] + " <aws_hosted_zone_id> ...")
sys.exit(1)
conn = route53.connect(
aws_access_key_id='<SOME_ACCESS_KEY>',
aws_secret_access_key='<THE_SECRET>'
)
match_id = ''.join(sys.argv[1:])
print("/* Creating Zone: " + match_id + " */")
zone = conn.get_hosted_zone_by_id(match_id)
print("resource \"aws_route53_zone\" \"" + zone.name.replace(".", "_")[:-1] + "\" {")
print(" name = \"" + zone.name[:-1] + "\"")
print("}")
print("")
num_records = 0
for record_set in zone.record_sets:
num_records += 1
if record_set.rrset_type == "A":
if record_set.is_alias_record_set():
alias_record(record_set)
else:
a_record(record_set)
if record_set.rrset_type == "TXT":
txt_record(record_set)
if record_set.rrset_type == "MX":
mx_record(record_set)
if record_set.rrset_type == "CNAME":
cname_record(record_set)
if record_set.rrset_type == "NS":
ns_record(record_set)
print("/* DNS Records: " + str(num_records) + " */")