forked from cloudmaniac/terraform-aws-static-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
362 lines (300 loc) · 10.1 KB
/
main.tf
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
## Providers definition
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.34.0"
}
}
}
provider "aws" {
alias = "us-east-1"
region = "us-east-1"
}
## Route 53
# Provides details about the zone
data "aws_route53_zone" "main" {
name = var.domains-zone-root
private_zone = false
}
## ACM (AWS Certificate Manager)
# Creates the wildcard certificate *.<yourdomain.com>
resource "aws_acm_certificate" "wildcard_website" {
provider = aws.us-east-1
domain_name = var.domains-zone-root
subject_alternative_names = ["*.${var.domains-zone-root}"]
validation_method = "DNS"
tags = merge(var.tags, {
ManagedBy = "terraform"
Changed = formatdate("YYYY-MM-DD hh:mm ZZZ", timestamp())
})
lifecycle {
ignore_changes = [tags["Changed"]]
}
}
# Validates the ACM wildcard by creating a Route53 record (as `validation_method` is set to `DNS` in the aws_acm_certificate resource)
resource "aws_route53_record" "wildcard_validation" {
for_each = {
for dvo in aws_acm_certificate.wildcard_website.domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
}
}
name = each.value.name
type = each.value.type
zone_id = data.aws_route53_zone.main.zone_id
records = [each.value.record]
allow_overwrite = true
ttl = "60"
}
# Triggers the ACM wildcard certificate validation event
resource "aws_acm_certificate_validation" "wildcard_cert" {
provider = aws.us-east-1
certificate_arn = aws_acm_certificate.wildcard_website.arn
validation_record_fqdns = [for k, v in aws_route53_record.wildcard_validation : v.fqdn]
}
# Get the ARN of the issued certificate
data "aws_acm_certificate" "wildcard_website" {
provider = aws.us-east-1
depends_on = [
aws_acm_certificate.wildcard_website,
aws_route53_record.wildcard_validation,
aws_acm_certificate_validation.wildcard_cert,
]
domain = var.website-domain-main
statuses = ["ISSUED"]
most_recent = true
}
## S3
# Creates bucket to store logs
resource "aws_s3_bucket" "website_logs" {
bucket = "${var.website-domain-main}-logs"
acl = "log-delivery-write"
# Comment the following line if you are uncomfortable with Terraform destroying the bucket even if this one is not empty
force_destroy = true
tags = merge(var.tags, {
ManagedBy = "terraform"
Changed = formatdate("YYYY-MM-DD hh:mm ZZZ", timestamp())
})
lifecycle {
ignore_changes = [tags["Changed"]]
}
}
# Creates bucket to store the static website
resource "aws_s3_bucket" "website_root" {
bucket = "${var.website-domain-main}-root"
acl = "public-read"
# Comment the following line if you are uncomfortable with Terraform destroying the bucket even if not empty
force_destroy = true
logging {
target_bucket = aws_s3_bucket.website_logs.bucket
target_prefix = "${var.website-domain-main}/"
}
website {
index_document = "index.html"
error_document = var.support-spa ? "" : "404.html"
}
tags = merge(var.tags, {
ManagedBy = "terraform"
Changed = formatdate("YYYY-MM-DD hh:mm ZZZ", timestamp())
})
lifecycle {
ignore_changes = [tags["Changed"]]
}
}
# Creates bucket for the website handling the redirection (if required), e.g. from https://www.example.com to https://example.com
resource "aws_s3_bucket" "website_redirect" {
bucket = "${var.website-domain-main}-redirect"
acl = "public-read"
force_destroy = true
logging {
target_bucket = aws_s3_bucket.website_logs.bucket
target_prefix = "${var.website-domain-main}-redirect/"
}
website {
redirect_all_requests_to = "https://${var.website-domain-main}"
}
tags = merge(var.tags, {
ManagedBy = "terraform"
Changed = formatdate("YYYY-MM-DD hh:mm ZZZ", timestamp())
})
lifecycle {
ignore_changes = [tags["Changed"]]
}
}
## CloudFront
# Creates the CloudFront distribution to serve the static website
resource "aws_cloudfront_distribution" "website_cdn_root" {
enabled = true
price_class = "PriceClass_All"
# Select the correct PriceClass depending on who the CDN is supposed to serve (https://docs.aws.amazon.com/AmazonCloudFront/ladev/DeveloperGuide/PriceClass.html)
aliases = concat([var.website-domain-main], var.website-additional-domains)
origin {
origin_id = "origin-bucket-${aws_s3_bucket.website_root.id}"
domain_name = aws_s3_bucket.website_root.website_endpoint
custom_origin_config {
origin_protocol_policy = "http-only"
# The protocol policy that you want CloudFront to use when fetching objects from the origin server (a.k.a S3 in our situation). HTTP Only is the default setting when the origin is an Amazon S3 static website hosting endpoint, because Amazon S3 doesn’t support HTTPS connections for static website hosting endpoints.
http_port = 80
https_port = 443
origin_ssl_protocols = ["TLSv1.2", "TLSv1.1", "TLSv1"]
}
}
default_root_object = "index.html"
logging_config {
bucket = aws_s3_bucket.website_logs.bucket_domain_name
prefix = "${var.website-domain-main}/"
}
default_cache_behavior {
allowed_methods = ["GET", "HEAD", "OPTIONS"]
cached_methods = ["GET", "HEAD", "OPTIONS"]
target_origin_id = "origin-bucket-${aws_s3_bucket.website_root.id}"
min_ttl = "0"
default_ttl = "300"
max_ttl = "1200"
viewer_protocol_policy = "redirect-to-https" # Redirects any HTTP request to HTTPS
compress = true
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
# An optional AWS Lambda Function for customising CloudFront behaviour.
dynamic "lambda_function_association" {
for_each = var.cloudfront_lambda_function_arn == null ? [] : [1]
content {
event_type = var.cloudfront_lambda_function_event_type
lambda_arn = var.cloudfront_lambda_function_arn
}
}
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
viewer_certificate {
acm_certificate_arn = data.aws_acm_certificate.wildcard_website.arn
ssl_support_method = "sni-only"
}
custom_error_response {
error_caching_min_ttl = 300
error_code = 404
response_page_path = var.support-spa ? "/index.html" : "/404.html"
response_code = var.support-spa ? 200 : 404
}
tags = merge(var.tags, {
ManagedBy = "terraform"
Changed = formatdate("YYYY-MM-DD hh:mm ZZZ", timestamp())
})
lifecycle {
ignore_changes = [
tags["Changed"],
viewer_certificate,
]
}
}
# Creates the DNS record to point on the main CloudFront distribution ID
resource "aws_route53_record" "website_cdn_root_record" {
zone_id = data.aws_route53_zone.main.zone_id
name = var.website-domain-main
type = "A"
alias {
name = aws_cloudfront_distribution.website_cdn_root.domain_name
zone_id = aws_cloudfront_distribution.website_cdn_root.hosted_zone_id
evaluate_target_health = false
}
}
# Creates policy to allow public access to the S3 bucket
resource "aws_s3_bucket_policy" "update_website_root_bucket_policy" {
bucket = aws_s3_bucket.website_root.id
policy = <<POLICY
{
"Version": "2012-10-17",
"Id": "PolicyForWebsiteEndpointsPublicContent",
"Statement": [
{
"Sid": "PublicRead",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject"
],
"Resource": [
"${aws_s3_bucket.website_root.arn}/*",
"${aws_s3_bucket.website_root.arn}"
]
}
]
}
POLICY
}
# Creates the CloudFront distribution to serve the redirection website (if redirection is required)
resource "aws_cloudfront_distribution" "website_cdn_redirect" {
enabled = true
price_class = "PriceClass_All"
# Select the correct PriceClass depending on who the CDN is supposed to serve (https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/PriceClass.html)
aliases = [var.website-domain-redirect]
origin {
origin_id = "origin-bucket-${aws_s3_bucket.website_redirect.id}"
domain_name = aws_s3_bucket.website_redirect.website_endpoint
custom_origin_config {
http_port = 80
https_port = 443
origin_protocol_policy = "http-only"
origin_ssl_protocols = ["TLSv1", "TLSv1.1", "TLSv1.2"]
}
}
logging_config {
bucket = aws_s3_bucket.website_logs.bucket_domain_name
prefix = "${var.website-domain-main}-redirect/"
}
default_cache_behavior {
allowed_methods = ["GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT", "DELETE"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "origin-bucket-${aws_s3_bucket.website_redirect.id}"
min_ttl = "0"
default_ttl = "300"
max_ttl = "1200"
viewer_protocol_policy = "redirect-to-https" # Redirects any HTTP request to HTTPS
compress = true
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
viewer_certificate {
acm_certificate_arn = data.aws_acm_certificate.wildcard_website.arn
ssl_support_method = "sni-only"
}
tags = merge(var.tags, {
ManagedBy = "terraform"
Changed = formatdate("YYYY-MM-DD hh:mm ZZZ", timestamp())
})
lifecycle {
ignore_changes = [
tags["Changed"],
viewer_certificate,
]
}
}
# Creates the DNS record to point on the CloudFront distribution ID that handles the redirection website
resource "aws_route53_record" "website_cdn_redirect_record" {
zone_id = data.aws_route53_zone.main.zone_id
name = var.website-domain-redirect
type = "A"
alias {
name = aws_cloudfront_distribution.website_cdn_redirect.domain_name
zone_id = aws_cloudfront_distribution.website_cdn_redirect.hosted_zone_id
evaluate_target_health = false
}
}