This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdn.tf
174 lines (145 loc) · 4.73 KB
/
cdn.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
# Global Certificate
resource "aws_acm_certificate" "global_cert" {
provider = aws.virginia
domain_name = var.hz_main_name
subject_alternative_names = [format("*.%s", var.hz_main_name)]
validation_method = "DNS"
}
## ACM Validation
resource "aws_acm_certificate_validation" "global_cert_validate" {
provider = aws.virginia
certificate_arn = aws_acm_certificate.global_cert.arn
validation_record_fqdns = [for record in aws_route53_record.hz_main_record_certverify : record.fqdn]
}
# CloudFront Distribution
## OAIs
resource "aws_cloudfront_origin_access_identity" "fe_customer_oai" {
}
resource "aws_cloudfront_origin_access_identity" "fe_store_oai" {
}
## Functions
resource "aws_cloudfront_function" "fe_func_replace_request_uri" {
name = "elon-kiosk-cloudfront-function-replace-request-uri"
runtime = "cloudfront-js-1.0"
code = file("./cloudfront-functions/replace-request-uri.js")
}
## Distributions
resource "aws_cloudfront_distribution" "fe_distribution" {
aliases = [var.hz_main_name]
enabled = true
is_ipv6_enabled = true
price_class = "PriceClass_200"
restrictions {
geo_restriction {
restriction_type = "none"
}
}
viewer_certificate {
acm_certificate_arn = aws_acm_certificate.global_cert.arn
ssl_support_method = "sni-only"
}
#############################################
# Precedence 0) ALB
#############################################
# Origin
origin {
domain_name = aws_alb.alb_main.dns_name
origin_id = aws_alb.alb_main.id
custom_origin_config {
http_port = 80
https_port = 443
origin_protocol_policy = "match-viewer"
origin_ssl_protocols = ["TLSv1.2"]
}
}
# Behavior
ordered_cache_behavior {
path_pattern = "/api*"
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = aws_alb.alb_main.id
forwarded_values {
query_string = true # Forward all query_string
headers = ["*"] # Forward all headers
cookies {
forward = "all" # Forward all cookies
}
}
min_ttl = 0
default_ttl = 3600
max_ttl = 86400
viewer_protocol_policy = "redirect-to-https"
}
#############################################
# Precedence 1) S3:customer webview bucket
#############################################
# Origin
origin {
domain_name = aws_s3_bucket.wv_customer_bucket.bucket_domain_name
origin_id = aws_s3_bucket.wv_customer_bucket.id
s3_origin_config {
origin_access_identity = aws_cloudfront_origin_access_identity.fe_customer_oai.cloudfront_access_identity_path
}
}
# Behavior
ordered_cache_behavior {
path_pattern = "/store*" # Use "/store" prefix to make URL path like "/store/$STORE_ID"
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = aws_s3_bucket.wv_customer_bucket.id
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
function_association {
event_type = "viewer-request"
function_arn = aws_cloudfront_function.fe_func_replace_request_uri.arn
}
viewer_protocol_policy = "redirect-to-https"
min_ttl = 0
default_ttl = 3600
max_ttl = 86400
}
#############################################
# Default precedence) S3:store webview bucket
#############################################
default_root_object = "index.html"
# Origin
origin {
domain_name = aws_s3_bucket.wv_store_bucket.bucket_domain_name
origin_id = aws_s3_bucket.wv_store_bucket.id
origin_path = "/build"
s3_origin_config {
origin_access_identity = aws_cloudfront_origin_access_identity.fe_store_oai.cloudfront_access_identity_path
}
}
# Behavior
default_cache_behavior {
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = aws_s3_bucket.wv_store_bucket.id
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
viewer_protocol_policy = "redirect-to-https"
min_ttl = 0
default_ttl = 3600
max_ttl = 86400
}
# SPA support: 404 handling
custom_error_response {
error_code = 404
response_code = 200
response_page_path = "/index.html"
}
custom_error_response {
error_code = 403
response_code = 200
response_page_path = "/index.html"
}
}