-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws-s3-static-web-host.sh
executable file
·239 lines (205 loc) · 6.39 KB
/
aws-s3-static-web-host.sh
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/bin/bash
set -eo pipefail
if [[ "$TRACE" ]]; then
: ${START_TIME:=$(date +%s)}
export START_TIME
export PS4='+ [TRACE $BASH_SOURCE:$LINENO][ellapsed: $(( $(date +%s) - $START_TIME ))] '
set -x
fi
debug() {
[[ "$DEBUG" ]] && echo "-----> $*" 1>&2
}
index_html() {
cat > index.html <<EOF
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>$MYDOMAIN</title>
</head>
<body>
<h1>Welcome to $MYDOMAIN</h1>
<p>soon ...</p>
</body>
</html>
EOF
}
redirect_www() {
cat <<EOF
{
"RedirectAllRequestsTo": {
"HostName": "$MYDOMAIN",
"Protocol": "http"
}
}
EOF
}
create_buckets() {
declare desc="creates 2 buckets www and base"
declare redirectFile=$1
debug "$desc"
for bucket in $MYDOMAIN www.$MYDOMAIN; do
if aws s3api get-bucket-location --bucket $bucket;then
debug "bucket: $bucket already exsts ..."
continue
fi
aws s3 mb s3://$bucket
index_html
aws s3 cp --acl public-read index.html s3://$bucket
aws s3 website s3://$bucket/ --index-document index.html
debug "You can open: http://$MYDOMAIN.s3.amazonaws.com/index.html"
done
debug "redirect all www.$MYDOMAIN => $MYDOMAIN"
aws s3api put-bucket-website --bucket www.$MYDOMAIN --website-configuration file://<(redirect_www)
}
# For DNS aliases use simple A records and point to region specific s3 endpoint
# The region specific s3 endpoint dns nameserver's corresponding HostedZoneIds listed:
# http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_website_region_endpoints
recordset_alias() {
declare domainAlias=$1
: ${domainAlias:? required}
cat <<EOF
{
"Changes": [
{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "$domainAlias",
"Type": "A",
"AliasTarget": {
"HostedZoneId": "Z21DNDUVLTQW6Q",
"DNSName": "s3-website.eu-central-1.amazonaws.com.",
"EvaluateTargetHealth": false
}
}
}
]
}
EOF
}
get_hosted_zone_id() {
aws route53 list-hosted-zones-by-name \
--dns-name $MYDOMAIN \
--query HostedZones[0].Id \
--out text
}
create_dns() {
declare desc="creates route53 hosted zone, and aliases"
debug "$desc"
DOMAINID=$(get_hosted_zone_id)
if ! [[ "$DOMAINID" ]]; then
debug "Creating hosted zone for: $MYDOMAIN"
aws route53 create-hosted-zone \
--name $MYDOMAIN \
--caller-reference $(date +%Y-%m-%d--%H%M)
fi
DOMAINID=$(get_hosted_zone_id)
DOMAINID=${DOMAINID##*/}
debug "DomainId: $DOMAINID"
for domainAlias in "${MYDOMAIN}." "www.${MYDOMAIN}."; do
debug "creating alias for: $domainAlias"
local existingRecord=$(aws route53 list-resource-record-sets --hosted-zone-id $DOMAINID --query 'ResourceRecordSets[? Name ==`'$domainAlias'` && Type == `A`].Name' --out text)
debug existingRecord=$existingRecord
if [[ "$existingRecord" ]]; then
debug "DNS A record for: $domainAlias already exists ..."
continue
fi
aws route53 change-resource-record-sets \
--hosted-zone-id $DOMAINID \
--change-batch file://<(recordset_alias "${domainAlias}" )
done
debug "Set nameserver at your domain registrar:"
aws route53 get-hosted-zone --id $DOMAINID --query DelegationSet.NameServers --out text|xargs -n 1
#list AWS hosted domainnames:
#aws route53domains list-domains --region us-east-1 --query Domains[].DomainName --out text
}
register_json() {
: ${AutoRenew:=false}
: ${DurationInYears:=1}
: ${FirstName:? required}
: ${LastName:? required}
: ${ContactType:? required}
: ${OrganizationName:? required}
: ${AddressLine1:? required}
: ${AddressLine2:? required}
: ${City:? required}
: ${State:? required}
: ${CountryCode:? required}
: ${ZipCode:? required}
: ${PhoneNumber:? required}
: ${Email:? required}
cat <<EOF
{
"DomainName": "$MYDOMAIN",
"DurationInYears": 1,
"AutoRenew": ${AutoRenew},
"AdminContact": {
"FirstName": "${FirstName}",
"LastName": "${LastName}",
"ContactType": "${ContactType}",
"OrganizationName": "${OrganizationName}",
"AddressLine1": "${AddressLine1}",
"AddressLine2": "${AddressLine2}",
"City": "${City}",
"CountryCode": "${CountryCode}",
"ZipCode": "${ZipCode}",
"PhoneNumber": "${PhoneNumber}",
"Email": "${Email}"
},
"RegistrantContact": {
"FirstName": "${FirstName}",
"LastName": "${LastName}",
"ContactType": "${ContactType}",
"OrganizationName": "${OrganizationName}",
"AddressLine1": "${AddressLine1}",
"AddressLine2": "${AddressLine2}",
"City": "${City}",
"CountryCode": "${CountryCode}",
"ZipCode": "${ZipCode}",
"PhoneNumber": "${PhoneNumber}",
"Email": "${Email}"
},
"TechContact": {
"FirstName": "${FirstName}",
"LastName": "${LastName}",
"ContactType": "${ContactType}",
"OrganizationName": "${OrganizationName}",
"AddressLine1": "${AddressLine1}",
"AddressLine2": "${AddressLine2}",
"City": "${City}",
"CountryCode": "${CountryCode}",
"ZipCode": "${ZipCode}",
"PhoneNumber": "${PhoneNumber}",
"Email": "${Email}"
},
"PrivacyProtectAdminContact": true,
"PrivacyProtectRegistrantContact": true,
"PrivacyProtectTechContact": true
}
EOF
}
register_domain() {
declare desc="register domain at AWS"
local availabilty=$(
aws route53domains check-domain-availability --region us-east-1 --domain-name $MYDOMAIN --query Availability --out text
)
debug "$MYDOMAIN is: $availabilty"
if [[ "$availabilty" != "AVAILABLE" ]];then
echo "=====> Upps, you have missed it $MYDOMAIN is $availabilty"
return
fi
if aws route53domains list-domains --region us-east-1 --query 'Domains[].DomainName' --out text | grep -q $MYDOMAIN; then
debug "You g=have already registered: $MYDOMAIN ..."
return
fi
register_json > register.json
aws route53domains register-domain \
--region us-east-1 \
--cli-input-json file://register.json
}
main() {
: ${DEBUG:=1}
: ${MYDOMAIN:? reuired}
register_domain
create_buckets
create_dns
}
[[ "$0" == "$BASH_SOURCE" ]] && main "$@" || true