-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrun_create_cloudfront.py
executable file
·179 lines (152 loc) · 4.7 KB
/
run_create_cloudfront.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
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
import json
from argparse import ArgumentParser
from run_common import AWSCli
from run_common import _confirm_phase
from run_common import print_message
################################################################################
#
# start
#
################################################################################
def parse_args():
parser = ArgumentParser()
parser.add_argument('-f', '--force', action='store_true', help='pass confirm')
parser.add_argument('-b', '--bucket', type=str, required=True, help='s3 bucket name')
parser.add_argument('-e', '--endpoint', type=str, required=True, help='s3 bucket end point')
parser.add_argument('-a', '--acm-arn', dest='acm_arn', type=str, required=True, help='acm arn')
parser.add_argument('-c', '--cname', type=str, required=True, nargs='+', help='cname')
args = parser.parse_args()
if not args.force:
_confirm_phase()
return args
def is_exist_cname(cname_list):
aws_cli = AWSCli()
cmd = ['cloudfront', 'list-distributions']
rr = aws_cli.run(cmd)
for vv in rr['DistributionList']['Items']:
for cc in cname_list:
if 'Items' in vv['Aliases'] and cc in vv['Aliases']['Items']:
print_message('exist cname(%s)' % cc)
return True
return False
def create_cloudfront(bucket_name, origin_url, certificate_arn, cname_list):
origin_id = 'S3-Website-%s' % bucket_name
dd = dict()
dd['PriceClass'] = 'PriceClass_200'
dd['CallerReference'] = bucket_name
dd['Comment'] = ''
dd['Enabled'] = True
dd['Aliases'] = {
'Quantity': len(cname_list),
'Items': cname_list
}
dd['Origins'] = {
'Quantity': 1,
'Items': [
{
'Id': origin_id,
'DomainName': origin_url,
'CustomOriginConfig': {
'HTTPPort': 80,
'HTTPSPort': 443,
'OriginProtocolPolicy': 'http-only',
'OriginSslProtocols': {
'Items': [
'TLSv1',
'TLSv1.1',
'TLSv1.2'
],
'Quantity': 3
}
}
}
]
}
dd['DefaultCacheBehavior'] = {
'TargetOriginId': origin_id,
'ViewerProtocolPolicy': 'redirect-to-https',
'AllowedMethods': {
'CachedMethods': {
'Items': ['HEAD', 'GET'],
'Quantity': 2
},
'Items': ['HEAD', 'GET'],
'Quantity': 2
},
'Compress': False,
'ForwardedValues': {
'QueryString': False,
'Cookies': {
'Forward': 'none'
},
'Headers': {
'Quantity': 0
},
'QueryStringCacheKeys': {
'Quantity': 0
}
},
'TrustedSigners': {
'Enabled': False,
'Quantity': 0
},
"MinTTL": 0,
"DefaultTTL": 86400,
"MaxTTL": 31536000
}
dd['ViewerCertificate'] = {
'ACMCertificateArn': certificate_arn,
'Certificate': certificate_arn,
'CertificateSource': 'acm',
'MinimumProtocolVersion': 'TLSv1.1_2016',
'SSLSupportMethod': 'sni-only'
}
aws_cli = AWSCli()
cmd = ['cloudfront', 'create-distribution']
cmd += ['--distribution-config', json.dumps(dd)]
rr = aws_cli.run(cmd)
return rr
def create_route53(name, type, dns_name, alias_hostzon_id):
rrs = dict()
if alias_hostzon_id:
rrs = {
"Name": name,
"Type": type,
"AliasTarget": {
"HostedZoneId": alias_hostzon_id,
"DNSName": dns_name,
"EvaluateTargetHealth": False
}
}
else:
rrs = {
"Name": name,
"Type": type,
"ResourceRecord": [
{
"Value": dns_name
}
]
}
dd = dict()
dd['Changes'] = [
{
"Action": "CREATE",
"ResourceRecordSet": rrs
}
]
aws_cli = AWSCli()
cmd = ['route53', 'change-resource-record-sets']
cmd += ['--change-batch', json.dumps(dd)]
rr = aws_cli.run(cmd)
return rr
if __name__ == '__main__':
args = parse_args()
bucket_name = args.bucket
origin_domain_name = args.endpoint
certificate_arn = args.acm_arn
cname_list = args.cname
if is_exist_cname(cname_list):
exit(1)
create_cloudfront(bucket_name, origin_domain_name, certificate_arn, cname_list)