forked from milliHQ/terraform-aws-next-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
311 lines (251 loc) · 9.88 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
locals {
# next-tf config
config_dir = trimsuffix(var.next_tf_dir, "/")
config_file = jsondecode(file("${local.config_dir}/config.json"))
lambdas = lookup(local.config_file, "lambdas", {})
static_files_archive = "${local.config_dir}/${lookup(local.config_file, "staticFilesArchive", "")}"
# Build the proxy config JSON
config_file_images = lookup(local.config_file, "images", {})
config_file_version = lookup(local.config_file, "version", 0)
static_routes_json = lookup(local.config_file, "staticRoutes", [])
routes_json = lookup(local.config_file, "routes", [])
lambda_routes_json = flatten([
for integration_key, integration in local.lambdas : [
lookup(integration, "route", "/")
]
])
prerenders_json = lookup(local.config_file, "prerenders", {})
proxy_config_json = jsonencode({
routes = local.routes_json
staticRoutes = local.static_routes_json
lambdaRoutes = local.lambda_routes_json
prerenders = local.prerenders_json
})
}
# Generates for each function a unique function name
resource "random_id" "function_name" {
for_each = local.lambdas
prefix = "${each.key}-"
byte_length = 4
}
#########
# Lambdas
#########
# Static deployment to S3 website
module "statics_deploy" {
source = "./modules/statics-deploy"
static_files_archive = local.static_files_archive
expire_static_assets = var.expire_static_assets
debug_use_local_packages = var.debug_use_local_packages
cloudfront_id = module.proxy.cloudfront_id
cloudfront_arn = module.proxy.cloudfront_arn
tags = var.tags
lambda_role_permissions_boundary = var.lambda_role_permissions_boundary
use_awscli_for_static_upload = var.use_awscli_for_static_upload
}
# Lambda
resource "aws_lambda_function" "this" {
for_each = local.lambdas
function_name = random_id.function_name[each.key].hex
description = "Managed by Terraform-next.js"
role = aws_iam_role.lambda[each.key].arn
handler = lookup(each.value, "handler", "")
runtime = lookup(each.value, "runtime", var.lambda_runtime)
memory_size = lookup(each.value, "memory", var.lambda_memory_size)
timeout = var.lambda_timeout
tags = var.tags
filename = "${local.config_dir}/${lookup(each.value, "filename", "")}"
source_code_hash = filebase64sha256("${local.config_dir}/${lookup(each.value, "filename", "")}")
dynamic "environment" {
for_each = length(var.lambda_environment_variables) > 0 ? [true] : []
content {
variables = var.lambda_environment_variables
}
}
depends_on = [aws_iam_role_policy_attachment.lambda_logs, aws_cloudwatch_log_group.this]
}
# Lambda invoke permission
resource "aws_lambda_permission" "current_version_triggers" {
for_each = local.lambdas
statement_id = "AllowInvokeFromApiGateway"
action = "lambda:InvokeFunction"
function_name = random_id.function_name[each.key].hex
principal = "apigateway.amazonaws.com"
source_arn = "${module.api_gateway.this_apigatewayv2_api_execution_arn}/*/*/*"
}
#############
# Api-Gateway
#############
locals {
integrations_keys = flatten([
for integration_key, integration in local.lambdas : [
"ANY ${lookup(integration, "route", "")}/{proxy+}"
]
])
integration_values = flatten([
for integration_key, integration in local.lambdas : {
lambda_arn = aws_lambda_function.this[integration_key].arn
payload_format_version = "2.0"
timeout_milliseconds = var.lambda_timeout * 1000
}
])
integrations = zipmap(local.integrations_keys, local.integration_values)
}
module "api_gateway" {
source = "terraform-aws-modules/apigateway-v2/aws"
version = "0.11.0"
name = var.deployment_name
description = "Managed by Terraform-next.js"
protocol_type = "HTTP"
create_api_domain_name = false
integrations = local.integrations
tags = var.tags
}
############
# Next/Image
############
# Permission for image optimizer to fetch images from S3 deployment
data "aws_iam_policy_document" "access_static_deployment" {
statement {
actions = ["s3:GetObject"]
resources = ["${module.statics_deploy.static_bucket_arn}/*"]
}
}
module "next_image" {
count = var.create_image_optimization ? 1 : 0
source = "dealmore/next-js-image-optimization/aws"
version = "~> 10.0.8"
cloudfront_create_distribution = false
# tf-next does not distinct between image and device sizes, because they
# are eventually merged together on the image optimizer.
# So we only use a single key (next_image_domains) to pass ALL (image &
# device) sizes to the optimizer and by setting the other
# (next_image_device_sizes) to an empty array which prevents the optimizer
# from adding the default device settings
next_image_domains = lookup(local.config_file_images, "domains", [])
next_image_image_sizes = lookup(local.config_file_images, "sizes", [])
next_image_device_sizes = []
source_bucket_id = module.statics_deploy.static_bucket_id
lambda_attach_policy_json = true
lambda_policy_json = data.aws_iam_policy_document.access_static_deployment.json
lambda_role_permissions_boundary = var.lambda_role_permissions_boundary
deployment_name = var.deployment_name
tags = var.tags
}
##################
# CloudFront Proxy
##################
locals {
next_image_cloudfront_origins = var.create_image_optimization ? [module.next_image[0].cloudfront_origin_image_optimizer] : []
proxy_cloudfront_origins = var.cloudfront_origins != null ? concat(
local.next_image_cloudfront_origins,
var.cloudfront_origins
) : local.next_image_cloudfront_origins
# Custom CloudFront beheaviour for image optimization
next_image_custom_behavior = var.create_image_optimization ? [{
path_pattern = "/_next/image*"
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
target_origin_id = module.next_image[0].cloudfront_origin_id
compress = true
viewer_protocol_policy = "redirect-to-https"
origin_request_policy_id = module.next_image[0].cloudfront_origin_request_policy_id
cache_policy_id = module.next_image[0].cloudfront_cache_policy_id
}] : []
cloudfront_custom_behaviors = var.cloudfront_custom_behaviors != null ? concat(
local.next_image_custom_behavior,
var.cloudfront_custom_behaviors
) : local.next_image_custom_behavior
}
resource "random_id" "policy_name" {
prefix = "${var.deployment_name}-"
byte_length = 4
}
resource "aws_cloudfront_origin_request_policy" "this" {
name = "${random_id.policy_name.hex}-origin"
comment = "Managed by Terraform Next.js"
cookies_config {
cookie_behavior = "all"
}
headers_config {
header_behavior = length(var.cloudfront_origin_headers) == 0 ? "none" : "whitelist"
dynamic "headers" {
for_each = length(var.cloudfront_origin_headers) == 0 ? [] : [true]
content {
items = var.cloudfront_origin_headers
}
}
}
query_strings_config {
query_string_behavior = "all"
}
}
resource "aws_cloudfront_cache_policy" "this" {
name = "${random_id.policy_name.hex}-cache"
comment = "Managed by Terraform Next.js"
# Default values (Should be provided by origin)
min_ttl = 0
default_ttl = 86400
max_ttl = 31536000
parameters_in_cache_key_and_forwarded_to_origin {
cookies_config {
cookie_behavior = "all"
}
headers_config {
header_behavior = length(var.cloudfront_cache_key_headers) == 0 ? "none" : "whitelist"
dynamic "headers" {
for_each = length(var.cloudfront_cache_key_headers) == 0 ? [] : [true]
content {
items = var.cloudfront_cache_key_headers
}
}
}
query_strings_config {
query_string_behavior = "all"
}
enable_accept_encoding_gzip = true
enable_accept_encoding_brotli = true
}
}
module "proxy" {
source = "./modules/proxy"
api_gateway_endpoint = trimprefix(module.api_gateway.this_apigatewayv2_api_api_endpoint, "https://")
static_bucket_endpoint = module.statics_deploy.static_bucket_endpoint
static_bucket_access_identity = module.statics_deploy.static_bucket_access_identity
proxy_config_json = local.proxy_config_json
proxy_config_version = local.config_file_version
# Forwarding variables
deployment_name = var.deployment_name
cloudfront_price_class = var.cloudfront_price_class
cloudfront_origins = local.proxy_cloudfront_origins
cloudfront_custom_behaviors = local.cloudfront_custom_behaviors
cloudfront_alias_domains = var.domain_names
cloudfront_viewer_certificate_arn = var.cloudfront_viewer_certificate_arn
cloudfront_minimum_protocol_version = var.cloudfront_minimum_protocol_version
cloudfront_origin_request_policy_id = aws_cloudfront_origin_request_policy.this.id
cloudfront_cache_policy_id = aws_cloudfront_cache_policy.this.id
debug_use_local_packages = var.debug_use_local_packages
tags = var.tags
lambda_role_permissions_boundary = var.lambda_role_permissions_boundary
providers = {
aws.global_region = aws.global_region
}
}
################
# Custom Domains
################
data "aws_route53_zone" "alias_domains" {
count = var.create_domain_name_records ? length(var.domain_zone_names) : 0
name = var.domain_zone_names[count.index]
}
resource "aws_route53_record" "alias_domains" {
count = var.create_domain_name_records ? length(var.domain_names) : 0
zone_id = data.aws_route53_zone.alias_domains[count.index].zone_id
name = var.domain_names[count.index]
type = "A"
alias {
name = module.proxy.cloudfront_domain_name
zone_id = module.proxy.cloudfront_hosted_zone_id
evaluate_target_health = false
}
}