-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwebsite-script.js
More file actions
235 lines (218 loc) · 6.87 KB
/
website-script.js
File metadata and controls
235 lines (218 loc) · 6.87 KB
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
exports.script = function websiteScript(s3, route53, path, fs, domainName) {
const publicPolicy = (
`{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::${domainName}/*"
}
]
}`
);
const wname = 'www.' + domainName;
// CREATE THE ROOT S3 BUCKET
var bucketParams = {
Bucket : domainName,
ACL : 'public-read'
};
var staticHostParams = {
Bucket: domainName,
WebsiteConfiguration: {
ErrorDocument: {
Key: 'error.html'
},
IndexDocument: {
Suffix: 'index.html'
},
}
};
s3.createBucket(bucketParams, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Root Bucket URL is ", data.Location);
s3.putBucketWebsite(staticHostParams, function(err, data) {
if (err) {
console.log("Error", err);
} else {
//ATTACH THE PUBLIC BUCKET POLICY
var params = {
Bucket: domainName,
Policy: publicPolicy
};
s3.putBucketPolicy(params, function(err, data) {
if (err){
console.log(err, err.stack);
}
else{
console.log("public bucket policy attached")
}
});
//UPLOAD THE WEBSITE TEMPLATE TO THE S3 BUCKET
const uploadDir = function(s3Path, bucketName) {
function walkSync(currentDirPath, callback) {
fs.readdirSync(currentDirPath).forEach(function (name) {
var filePath = path.join(currentDirPath, name);
var stat = fs.statSync(filePath);
if (stat.isFile()) {
callback(filePath, stat);
}
else if (stat.isDirectory()) {
walkSync(filePath, callback);
}
});
}
walkSync(s3Path, function(filePath, stat) {
let bucketPath = filePath.substring(s3Path.length+1);
let fext = bucketPath.substring(bucketPath.lastIndexOf('.') + 1);
let content_type = '';
if(fext =='svg'){
content_type = 'image/svg+xml'
}
else if(fext =='jpg' || fext =='jpeg'){
content_type = 'image/jpeg'
}
else if(fext =='png'){
content_type = 'image/png'
}
else if(fext =='html'){
content_type = 'text/html'
}
else if(fext =='css'){
content_type = 'text/css'
}
else if(fext =='js'){
content_type = 'application/javascript'
}
else if(fext =='txt'){
content_type = 'text/plain'
}
else if(fext =='xml'){
content_type = 'text/xml'
}
else if(fext =='mp4'){
content_type = 'video/mp4'
}
let fileParams = {Bucket: bucketName, Key: bucketPath, Body: fs.readFileSync(filePath), ContentType: content_type};
s3.putObject(fileParams, function(err, data) {
if (err) {
console.log(err)
}
else {
console.log(`Successfully uploaded ${bucketPath} to ${bucketName}`);
}
});
});
};
uploadDir("website_template", domainName);
// CREATE THE WWW REROUTE BUCKET
var wbucketParams = {
Bucket : wname,
};
var wstaticHostParams = {
Bucket: wname,
WebsiteConfiguration: {
RedirectAllRequestsTo: {
HostName: domainName, /* required */
Protocol: 'http'
},
}
};
s3.createBucket(wbucketParams, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("WWW Bucket URL is ", data.Location);
s3.putBucketWebsite(wstaticHostParams, function(err, data) {
if (err) {
console.log("Error", err);
}
else {
// Create Route53 Hosted Zone
var call_ref = new Date().toString();
var hostZoneParams = {
CallerReference: call_ref, /* required */
Name: domainName, /* required */
HostedZoneConfig: {
Comment: 'almost hosted zone',
PrivateZone: false
}
};
route53.createHostedZone(hostZoneParams, function(err, data) {
if (err) {
console.log(err, err.stack);
}
else {
console.log('In your Domain name registrar, please change your DNS settings to custom DNS and add the following Nameservers:')
console.log(data.DelegationSet.NameServers);
const hosted_id = data.HostedZone.Id.slice(data.HostedZone.Id.lastIndexOf('/') + 1);
// Create the Alias A record for the root bucket
var aliasParams = {
ChangeBatch: {
Changes: [{
Action: "CREATE",
ResourceRecordSet: {
AliasTarget: {
DNSName: `s3-website-${process.env.AWS_REGION}.amazonaws.com`,
EvaluateTargetHealth: false,
HostedZoneId: 'Z3AQBSTGFYJSTF' // a code depending on your region and resource for more info refer to https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_website_region_endpoints
},
Name: domainName,
Type: "A"
}
}],
Comment: "Alias Record for S3 Bucket"
},
HostedZoneId: hosted_id // the Id of your recently created hosted zone
};
route53.changeResourceRecordSets(aliasParams, function(err, data) {
if (err) {
console.log(err, err.stack);
}
else {
console.log('Succesfully created the alias A record for the root bucket');
//Create the Alias A record for the www bucket
var waliasParams = {
ChangeBatch: {
Changes: [{
Action: "CREATE",
ResourceRecordSet: {
AliasTarget: {
DNSName: `s3-website-${process.env.AWS_REGION}.amazonaws.com`,
EvaluateTargetHealth: false,
HostedZoneId: 'Z3AQBSTGFYJSTF' // a code depending on your region and resource for more info refer to https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_website_region_endpoints
},
Name: wname,
Type: "A"
}
}],
Comment: "Alias Record for S3 Bucket"
},
HostedZoneId: hosted_id // the Id of your recently created hosted zone
};
route53.changeResourceRecordSets(waliasParams, function(err, data) {
if (err) {
console.log(err, err.stack);
}
else {
console.log('Succesfully created the alias A record for the www bucket');
console.log('Please wait a couple of seconds ...')
}
});
}
});
}
});
}
});
}
});
}
});
}
});
}