This repository has been archived by the owner on Nov 24, 2021. It is now read-only.
forked from riboseinc/terraform-aws-s3-cloudfront-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudfront.tf
197 lines (154 loc) · 5.7 KB
/
cloudfront.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
# Since Terraform not support conditional in the resources atm
# https://github.com/hashicorp/terraform/issues/15974
# => we need to duplicate "aws_cloudfront_distribution" to support "lambda@edge" feature
resource "aws_cloudfront_distribution" "main" {
count = var.lambda_edge_enabled ? 0 : 1
is_ipv6_enabled = var.cf_ipv6_enabled
provider = aws.cloudfront
http_version = "http2"
origin {
origin_id = "origin-${var.fqdn}"
domain_name = aws_s3_bucket.main.website_endpoint
# https://docs.aws.amazon.com/AmazonCloudFront/latest/
# DeveloperGuide/distribution-web-values-specify.html
custom_origin_config {
# "HTTP Only: CloudFront uses only HTTP to access the origin."
# "Important: If your origin is an Amazon S3 bucket configured
# as a website endpoint, you must choose this option. Amazon S3
# doesn't support HTTPS connections for website endpoints."
origin_protocol_policy = "http-only"
http_port = "80"
https_port = "443"
# TODO: given the origin_protocol_policy set to `http-only`,
# not sure what this does...
# "If the origin is an Amazon S3 bucket, CloudFront always uses TLSv1.2."
origin_ssl_protocols = ["TLSv1.2"]
}
# s3_origin_config is not compatible with S3 website hosting, if this
# is used, /news/index.html will not resolve as /news/.
# https://www.reddit.com/r/aws/comments/6o8f89/can_you_force_cloudfront_only_access_while_using/
# s3_origin_config {
# origin_access_identity = "${aws_cloudfront_origin_access_identity.main.cloudfront_access_identity_path}"
# }
# Instead, we use a secret to authenticate CF requests to S3 policy.
# Not the best, but...
custom_header {
name = "User-Agent"
value = var.refer_secret
}
}
enabled = true
default_root_object = var.index_document
custom_error_response {
error_code = "404"
error_caching_min_ttl = "300"
response_code = var.single_page_application ? var.spa_error_response_code : var.error_response_code
response_page_path = "/${var.single_page_application ? var.index_document : var.error_document}"
}
aliases = concat([var.fqdn], var.aliases)
price_class = var.cloudfront_price_class
default_cache_behavior {
target_origin_id = "origin-${var.fqdn}"
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
compress = true
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
viewer_protocol_policy = "redirect-to-https"
min_ttl = 0
default_ttl = 300
max_ttl = 1200
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
viewer_certificate {
acm_certificate_arn = var.ssl_certificate_arn
ssl_support_method = "sni-only"
minimum_protocol_version = "TLSv1"
}
web_acl_id = var.web_acl_id
// is_ipv6_enabled (Optional) - Whether the IPv6 is enabled for the distribution.
}
resource "aws_cloudfront_distribution" "main-lambda-edge" {
count = var.lambda_edge_enabled ? 1 : 0
is_ipv6_enabled = var.cf_ipv6_enabled
provider = aws.cloudfront
http_version = "http2"
origin {
origin_id = "origin-${var.fqdn}"
domain_name = aws_s3_bucket.main.website_endpoint
# Alternative ways to set the domain, probably no longer necessary
# domain_name = "${aws_s3_bucket.main.bucket_domain_name}"
# domain_name = "${var.fqdn}.s3-website.${data.aws_region.main.name}.amazonaws.com"
custom_origin_config {
origin_protocol_policy = "http-only"
http_port = "80"
https_port = "443"
origin_ssl_protocols = ["TLSv1", "TLSv1.1", "TLSv1.2"]
}
# s3_origin_config is not compatible with S3 website hosting, if this
# is used, /news/index.html will not resolve as /news/.
# https://www.reddit.com/r/aws/comments/6o8f89/can_you_force_cloudfront_only_access_while_using/
# s3_origin_config {
# origin_access_identity = "${aws_cloudfront_origin_access_identity.main.cloudfront_access_identity_path}"
# }
# Instead, we use a secret to authenticate CF requests to S3 policy.
# Not the best, but...
custom_header {
name = "User-Agent"
value = var.refer_secret
}
}
enabled = true
default_root_object = var.index_document
custom_error_response {
error_code = "404"
error_caching_min_ttl = "300"
response_code = var.single_page_application ? var.spa_error_response_code : var.error_response_code
response_page_path = "/${var.single_page_application ? var.index_document : var.error_document}"
}
aliases = concat([var.fqdn], var.aliases)
price_class = "PriceClass_100"
default_cache_behavior {
target_origin_id = "origin-${var.fqdn}"
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
compress = true
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
viewer_protocol_policy = "redirect-to-https"
min_ttl = 0
default_ttl = 300
max_ttl = 1200
lambda_function_association {
event_type = "viewer-request"
lambda_arn = var.lambda_edge_arn_version
}
lambda_function_association {
event_type = "viewer-response"
lambda_arn = var.lambda_edge_arn_version
}
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
viewer_certificate {
acm_certificate_arn = var.ssl_certificate_arn
ssl_support_method = "sni-only"
minimum_protocol_version = "TLSv1"
}
web_acl_id = var.web_acl_id
}