-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcloudfront.tf
More file actions
180 lines (152 loc) · 4.8 KB
/
cloudfront.tf
File metadata and controls
180 lines (152 loc) · 4.8 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
resource "aws_cloudfront_distribution" "rko-router" {
provider = aws.use1
enabled = true
is_ipv6_enabled = true
http_version = "http2and3"
price_class = "PriceClass_All"
comment = "rko-router"
aliases = [
"rko-router.rubykaigi.org",
"rubykaigi.org",
"2006.rubykaigi.org",
"2007.rubykaigi.org",
"2008.rubykaigi.org",
"j.rubykaigi.org",
"regional.rubykaigi.org",
"sapporo.rubykaigi.org",
"sponsorship.rubykaigi.org",
"www.rubykaigi.org",
"yami.rubykaigi.org",
]
viewer_certificate {
acm_certificate_arn = data.aws_acm_certificate.use1-wild-rk-o.arn
minimum_protocol_version = "TLSv1.2_2021"
ssl_support_method = "sni-only"
}
logging_config {
include_cookies = false
bucket = "rk-aws-logs.s3.amazonaws.com"
prefix = "cf/rko-router.rubykaigi.org/"
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
origin {
origin_id = "rko-router-lambda"
domain_name = replace(aws_lambda_function_url.rko-router.function_url, "/^https:\\/+|\\/$/", "")
custom_origin_config {
http_port = 80
https_port = 443
origin_protocol_policy = "https-only"
origin_ssl_protocols = ["TLSv1.2"]
origin_keepalive_timeout = 30
origin_read_timeout = 35
}
origin_shield {
enabled = true
origin_shield_region = "us-west-2"
}
}
ordered_cache_behavior {
path_pattern = "/_csp"
allowed_methods = ["GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH", "DELETE"]
cached_methods = ["GET", "HEAD", "OPTIONS"]
target_origin_id = "rko-router-lambda"
viewer_protocol_policy = "https-only"
cache_policy_id = data.aws_cloudfront_cache_policy.Managed-CachingDisabled.id
response_headers_policy_id = aws_cloudfront_response_headers_policy.rko-router.id
function_association {
event_type = "viewer-request"
function_arn = aws_cloudfront_function.rko-router-viewreq.arn
}
}
default_cache_behavior {
allowed_methods = ["GET", "HEAD", "OPTIONS"]
cached_methods = ["GET", "HEAD", "OPTIONS"]
target_origin_id = "rko-router-lambda"
viewer_protocol_policy = "allow-all"
cache_policy_id = aws_cloudfront_cache_policy.rko-router-default.id
response_headers_policy_id = aws_cloudfront_response_headers_policy.rko-router.id
function_association {
event_type = "viewer-request"
function_arn = aws_cloudfront_function.rko-router-viewreq.arn
}
}
}
data "aws_cloudfront_cache_policy" "Managed-CachingDisabled" {
provider = aws.use1
name = "Managed-CachingDisabled"
}
resource "aws_cloudfront_cache_policy" "rko-router-default" {
provider = aws.use1
name = "rko-router-default"
comment = "rko-router-default"
min_ttl = 1
default_ttl = 86400
max_ttl = 31536000
parameters_in_cache_key_and_forwarded_to_origin {
enable_accept_encoding_brotli = true
enable_accept_encoding_gzip = true
headers_config {
header_behavior = "whitelist"
headers {
items = ["x-rko-host", "x-rko-xfp", "cloudfront-forwarded-proto"]
}
}
query_strings_config {
query_string_behavior = "none"
}
cookies_config {
cookie_behavior = "none"
}
}
}
resource "aws_cloudfront_response_headers_policy" "rko-router" {
name = "rko-router"
comment = "rko-router"
server_timing_headers_config {
enabled = true
sampling_rate = 100
}
# Function URL cannot return these headers directly and they get rewritten. Remove redundant headers.
remove_headers_config {
items {
header = "X-Amzn-Remapped-Connection"
}
items {
header = "X-Amzn-Remapped-Content-Length"
}
items {
header = "X-Amzn-Remapped-Server"
}
items {
header = "X-Amzn-Remapped-Date"
}
}
}
resource "null_resource" "tsc" {
triggers = {
tsdgst = join("", [
sha256(join("", [for f in fileset("${path.module}/cf_functions", "src/**/*.ts") : filesha256("${path.module}/cf_functions/${f}")])),
filesha256("${path.module}/cf_functions/pnpm-lock.yaml"),
])
}
provisioner "local-exec" {
command = "cd ${path.module}/cf_functions && pnpm i && tsc -b"
}
}
data "local_file" "viewreq" {
filename = "${path.module}/cf_functions/src/viewreq.js"
depends_on = [null_resource.tsc]
}
resource "aws_cloudfront_function" "rko-router-viewreq" {
provider = aws.use1
name = "rko-router-viewreq"
comment = "rko-router viewer-request"
runtime = "cloudfront-js-2.0"
publish = true
code = "${replace(data.local_file.viewreq.content, "/(?m)^export function handler/", "function handler")}\n// 1"
depends_on = [null_resource.tsc]
}