-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.tf
346 lines (308 loc) · 11.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
resource "aws_s3_bucket" "website" {
bucket = var.bucket_name
tags = {
Website = var.name
}
}
resource "aws_s3_bucket_acl" "website" {
bucket = aws_s3_bucket.website.id
acl = "public-read"
depends_on = [
aws_s3_bucket_ownership_controls.website,
aws_s3_bucket_public_access_block.website,
]
}
resource "aws_s3_bucket_public_access_block" "website" {
bucket = aws_s3_bucket.website.id
block_public_acls = false
block_public_policy = false
ignore_public_acls = false
restrict_public_buckets = false
}
resource "aws_s3_bucket_ownership_controls" "website" {
bucket = aws_s3_bucket.website.id
rule {
object_ownership = "BucketOwnerPreferred"
}
}
resource "aws_s3_bucket_website_configuration" "website" {
bucket = aws_s3_bucket.website.bucket
index_document {
suffix = var.index_document
}
error_document {
key = ("" == var.error_document) ? var.index_document : var.error_document
}
}
resource "aws_s3_bucket_cors_configuration" "website" {
count = (var.bucket_cors == true) ? 1 : 0
bucket = aws_s3_bucket.website.bucket
cors_rule {
allowed_headers = ["*"]
allowed_methods = ["POST", "GET", "PUT", "DELETE"]
allowed_origins = ["*"]
expose_headers = ["ETag"]
max_age_seconds = 3000
}
}
resource "aws_s3_bucket" "website_redirect_apex" {
count = var.apex_redirect ? 1 : 0
bucket = "www.${var.bucket_name}"
tags = {
Website = var.name
}
}
resource "aws_s3_bucket_acl" "website_redirect_apex" {
count = var.apex_redirect ? 1 : 0
bucket = aws_s3_bucket.website_redirect_apex[0].id
acl = "public-read"
depends_on = [
aws_s3_bucket_ownership_controls.website_redirect_apex[0],
aws_s3_bucket_public_access_block.website_redirect_apex[0],
]
}
resource "aws_s3_bucket_public_access_block" "website_redirect_apex" {
count = var.apex_redirect ? 1 : 0
bucket = aws_s3_bucket.website_redirect_apex[0].id
block_public_acls = false
block_public_policy = false
ignore_public_acls = false
restrict_public_buckets = false
}
resource "aws_s3_bucket_ownership_controls" "website_redirect_apex" {
count = var.apex_redirect ? 1 : 0
bucket = aws_s3_bucket.website_redirect_apex[0].id
rule {
object_ownership = "BucketOwnerPreferred"
}
}
resource "aws_s3_bucket_website_configuration" "website_redirect_apex" {
count = var.apex_redirect ? 1 : 0
bucket = aws_s3_bucket.website_redirect_apex[0].bucket
redirect_all_requests_to {
host_name = var.dns
protocol = "https"
}
}
resource "aws_cloudfront_distribution" "website" {
origin {
domain_name = aws_s3_bucket.website.website_endpoint
origin_id = local.origin_target_id
custom_origin_config {
// These are all the defaults.
http_port = "80"
https_port = "443"
origin_protocol_policy = "http-only"
origin_ssl_protocols = ["TLSv1", "TLSv1.1", "TLSv1.2"]
}
dynamic "custom_header" {
for_each = var.custom_origin_headers
content {
name = custom_header.value.name
value = custom_header.value.value
}
}
}
enabled = true
is_ipv6_enabled = true
comment = "Website ${var.name} Distribution"
default_root_object = var.default_root_object
aliases = [var.dns]
default_cache_behavior {
allowed_methods = ["GET", "HEAD", "OPTIONS"]
cached_methods = ["GET", "HEAD"]
target_origin_id = local.origin_target_id
dynamic "forwarded_values" {
for_each = (null != var.cache_policy_id) ? {} : { x : true }
content {
query_string = var.forward_query_string
cookies {
forward = "none"
}
headers = var.forwarded_headers
}
}
viewer_protocol_policy = "redirect-to-https"
min_ttl = 0
default_ttl = 3600
max_ttl = 86400
compress = true
cache_policy_id = var.cache_policy_id
origin_request_policy_id = var.origin_request_policy_id
response_headers_policy_id = var.response_headers_policy_id
dynamic "lambda_function_association" {
for_each = toset(var.lambdas)
content {
event_type = lambda_function_association.value.event_type
lambda_arn = lambda_function_association.value.lambda_arn
include_body = lambda_function_association.value.include_body
}
}
dynamic "function_association" {
for_each = { for i, l in var.functions : "function-${i}" => l }
content {
event_type = function_association.value.event_type
function_arn = function_association.value.function_arn
}
}
}
dynamic "ordered_cache_behavior" {
for_each = var.custom_behaviors != null ? var.custom_behaviors : []
content {
path_pattern = ordered_cache_behavior.value["path_pattern"]
allowed_methods = lookup(ordered_cache_behavior.value, "allowed_methods", ["GET", "HEAD"])
cached_methods = lookup(ordered_cache_behavior.value, "cached_methods", ["GET", "HEAD"])
target_origin_id = lookup(ordered_cache_behavior.value, "target_origin_id", local.origin_target_id)
compress = lookup(ordered_cache_behavior.value, "compress", true)
viewer_protocol_policy = lookup(ordered_cache_behavior.value, "viewer_protocol_policy", "redirect-to-https")
origin_request_policy_id = lookup(ordered_cache_behavior.value, "origin_request_policy_id", null)
cache_policy_id = lookup(ordered_cache_behavior.value, "cache_policy_id", null)
response_headers_policy_id = lookup(ordered_cache_behavior.value, "response_headers_policy_id", null)
}
}
price_class = var.price_class
restrictions {
geo_restriction {
restriction_type = length(var.geolocations) == 0 ? "none" : "whitelist"
locations = length(var.geolocations) == 0 ? null : var.geolocations
}
}
tags = {
}
viewer_certificate {
acm_certificate_arn = aws_acm_certificate_validation.cert.certificate_arn
ssl_support_method = "sni-only"
minimum_protocol_version = "TLSv1"
}
dynamic "custom_error_response" {
for_each = toset(("" == var.error_403_page_path) ? [] : [var.error_403_page_path])
content {
error_code = 403
response_code = var.error_403_page_code
response_page_path = custom_error_response.value
}
}
dynamic "custom_error_response" {
for_each = toset(("" == var.error_404_page_path) ? [] : [var.error_404_page_path])
content {
error_code = 404
response_code = var.error_404_page_code
response_page_path = custom_error_response.value
}
}
}
resource "aws_cloudfront_distribution" "website_redirect_apex" {
count = var.apex_redirect ? 1 : 0
origin {
domain_name = aws_s3_bucket.website_redirect_apex[count.index].website_endpoint
origin_id = local.origin_target_id
custom_origin_config {
// These are all the defaults.
http_port = "80"
https_port = "443"
origin_protocol_policy = "http-only"
origin_ssl_protocols = ["TLSv1", "TLSv1.1", "TLSv1.2"]
}
}
enabled = true
is_ipv6_enabled = true
comment = "Website ${var.name} Redirect to Apex Distribution"
aliases = ["www.${var.dns}"]
default_cache_behavior {
allowed_methods = ["GET", "HEAD", "OPTIONS"]
cached_methods = ["GET", "HEAD"]
target_origin_id = local.origin_target_id
forwarded_values {
query_string = var.forward_query_string
headers = var.forwarded_headers
cookies {
forward = "none"
}
}
viewer_protocol_policy = "redirect-to-https"
min_ttl = 0
default_ttl = 3600
max_ttl = 86400
compress = true
}
price_class = var.price_class
restrictions {
geo_restriction {
restriction_type = "none"
}
}
viewer_certificate {
acm_certificate_arn = aws_acm_certificate_validation.cert.certificate_arn
ssl_support_method = "sni-only"
minimum_protocol_version = "TLSv1"
}
}
resource "aws_route53_record" "website" {
zone_id = var.zone
name = var.dns
type = "A"
alias {
name = aws_cloudfront_distribution.website.domain_name
zone_id = aws_cloudfront_distribution.website.hosted_zone_id
evaluate_target_health = false
}
}
resource "aws_route53_record" "website_redirect_apex" {
count = var.apex_redirect ? 1 : 0
zone_id = var.zone
name = "www.${var.dns}"
type = "A"
alias {
name = aws_cloudfront_distribution.website_redirect_apex[count.index].domain_name
zone_id = aws_cloudfront_distribution.website_redirect_apex[count.index].hosted_zone_id
evaluate_target_health = false
}
}
resource "aws_acm_certificate" "cert" {
domain_name = var.dns
validation_method = "DNS"
provider = aws.acm
subject_alternative_names = var.apex_redirect ? [local.www_dns] : null
lifecycle {
create_before_destroy = true
}
}
resource "aws_route53_record" "cert_validation" {
allow_overwrite = var.can_overwrite
name = element(tolist(aws_acm_certificate.cert.domain_validation_options), 0).resource_record_name
type = element(tolist(aws_acm_certificate.cert.domain_validation_options), 0).resource_record_type
zone_id = var.zone
records = [element(tolist(aws_acm_certificate.cert.domain_validation_options), 0).resource_record_value]
ttl = 60
}
resource "aws_route53_record" "cert_validation_alt" {
allow_overwrite = var.can_overwrite
count = var.apex_redirect ? 1 : 0
name = element(tolist(aws_acm_certificate.cert.domain_validation_options), 1).resource_record_name
type = element(tolist(aws_acm_certificate.cert.domain_validation_options), 1).resource_record_type
zone_id = var.zone
records = [element(tolist(aws_acm_certificate.cert.domain_validation_options), 1).resource_record_value]
ttl = 60
}
resource "aws_acm_certificate_validation" "cert" {
provider = aws.acm
certificate_arn = aws_acm_certificate.cert.arn
validation_record_fqdns = var.apex_redirect ? [aws_route53_record.cert_validation.fqdn, aws_route53_record.cert_validation_alt[0].fqdn] : [aws_route53_record.cert_validation.fqdn]
}
resource "aws_s3_bucket_policy" "website" {
bucket = aws_s3_bucket.website.id
policy = data.aws_iam_policy_document.s3_website_policy.json
depends_on = [
aws_s3_bucket_ownership_controls.website,
aws_s3_bucket_public_access_block.website,
]
}
resource "aws_s3_bucket_policy" "website_redirect_apex" {
count = var.apex_redirect ? 1 : 0
bucket = aws_s3_bucket.website_redirect_apex[count.index].id
policy = data.aws_iam_policy_document.s3_website_redirect_apex_policy[count.index].json
depends_on = [
aws_s3_bucket_ownership_controls.website_redirect_apex[0],
aws_s3_bucket_public_access_block.website_redirect_apex[0],
]
}