-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
194 lines (160 loc) · 4.23 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
provider "aws" {
}
provider "aws" {
alias = "us_east_1"
region = "us-east-1"
}
resource "aws_cloudfront_origin_access_identity" "OAI" {
}
module "frontend" {
source = "./modules/frontend"
OAI_iam_arn = aws_cloudfront_origin_access_identity.OAI.iam_arn
}
resource "aws_s3_bucket" "files_bucket" {
force_destroy = "true"
}
module "api" {
source = "./modules/backend"
files_bucket_arn = aws_s3_bucket.files_bucket.arn
files_bucket_path = "files"
}
# lambda@edge
resource "random_id" "id" {
byte_length = 8
}
data "archive_file" "lambda_edge_zip" {
type = "zip"
output_path = "/tmp/${random_id.id.hex}-lambda_edge.zip"
source {
content = <<EOF
module.exports.handler = async (event) => {
const request = event.Records[0].cf.request;
request.uri = request.uri.replace(/^\/[^\/]*/, "");
return request;
};
EOF
filename = "main.js"
}
}
resource "aws_lambda_function" "lambda_edge" {
function_name = "${random_id.id.hex}-edge-function"
filename = data.archive_file.lambda_edge_zip.output_path
source_code_hash = data.archive_file.lambda_edge_zip.output_base64sha256
handler = "main.handler"
runtime = "nodejs12.x"
role = aws_iam_role.lambda_edge_exec.arn
provider = aws.us_east_1
publish = true
}
resource "aws_iam_role" "lambda_edge_exec" {
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": ["lambda.amazonaws.com", "edgelambda.amazonaws.com"]
},
"Effect": "Allow"
}
]
}
EOF
}
resource "aws_cloudfront_distribution" "distribution" {
origin {
domain_name = module.frontend.bucket_regional_domain_name
origin_id = "frontend"
s3_origin_config {
origin_access_identity = aws_cloudfront_origin_access_identity.OAI.cloudfront_access_identity_path
}
}
origin {
domain_name = replace(module.api.invoke_url, "/^https?://([^/]*).*/", "$1")
origin_id = "api"
origin_path = replace(module.api.invoke_url, "/^https?://[^/]*(/.*)$/", "$1")
custom_origin_config {
http_port = 80
https_port = 443
origin_protocol_policy = "https-only"
origin_ssl_protocols = ["TLSv1.2"]
}
}
origin {
domain_name = aws_s3_bucket.files_bucket.bucket_regional_domain_name
origin_id = "files_bucket"
}
enabled = true
default_root_object = "index.html"
default_cache_behavior {
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "frontend"
default_ttl = 0
min_ttl = 0
max_ttl = 0
forwarded_values {
query_string = true
cookies {
forward = "all"
}
}
viewer_protocol_policy = "redirect-to-https"
compress = true
}
ordered_cache_behavior {
path_pattern = "/api/*"
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "api"
default_ttl = 0
min_ttl = 0
max_ttl = 0
forwarded_values {
query_string = true
cookies {
forward = "all"
}
}
viewer_protocol_policy = "https-only"
compress = true
lambda_function_association {
event_type = "origin-request"
lambda_arn = aws_lambda_function.lambda_edge.qualified_arn
}
}
ordered_cache_behavior {
path_pattern = "/files/*"
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "files_bucket"
default_ttl = 0
min_ttl = 0
max_ttl = 0
forwarded_values {
query_string = true
cookies {
forward = "none"
}
}
viewer_protocol_policy = "https-only"
compress = true
lambda_function_association {
event_type = "origin-request"
lambda_arn = aws_lambda_function.lambda_edge.qualified_arn
}
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
viewer_certificate {
cloudfront_default_certificate = true
}
is_ipv6_enabled = true
}
output "domain_name" {
value = aws_cloudfront_distribution.distribution.domain_name
}