forked from milliHQ/terraform-aws-next-js-image-optimization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
178 lines (143 loc) · 5.1 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
locals {
name_suffix = var.name_suffix == null ? random_id.suffix.hex : var.name_suffix
}
resource "random_id" "suffix" {
byte_length = 4
}
###############
# Worker Lambda
###############
module "lambda_content" {
source = "dealmore/download/npm"
version = "1.0.0"
module_name = "@dealmore/tf-next-image-optimization"
module_version = var.next_image_version
path_to_file = "dist.zip"
use_local = var.debug_use_local_packages
}
module "image_optimizer" {
source = "terraform-aws-modules/lambda/aws"
version = "1.34.0"
function_name = "${var.name_prefix}-image-optimizer-${local.name_suffix}"
description = "${var.name_prefix} nextjs image optimizer managed by terraform"
handler = "handler.handler"
runtime = "nodejs14.x"
memory_size = var.lambda_memory_size
timeout = var.lambda_timeout
publish = true
environment_variables = {
NODE_ENV = "production",
TF_NEXTIMAGE_DOMAINS = jsonencode(var.next_image_domains)
TF_NEXTIMAGE_DEVICE_SIZES = var.next_image_device_sizes != null ? jsonencode(var.next_image_device_sizes) : null
TF_NEXTIMAGE_IMAGE_SIZES = var.next_image_image_sizes != null ? jsonencode(var.next_image_image_sizes) : null
TF_NEXTIMAGE_SOURCE_BUCKET = var.source_bucket_id
}
create_package = false
local_existing_package = var.next_image_package_abs_path != null ? var.next_image_package_abs_path : module.lambda_content.abs_path
allowed_triggers = {
AllowExecutionFromAPIGateway = {
service = "apigateway"
arn = module.api_gateway.this_apigatewayv2_api_execution_arn
}
}
cloudwatch_logs_retention_in_days = 30
attach_policy_json = var.lambda_attach_policy_json
policy_json = var.lambda_policy_json
role_permissions_boundary = var.lambda_role_permissions_boundary
tags = var.tags
}
#########################
# API Gateway integration
#########################
module "api_gateway" {
source = "terraform-aws-modules/apigateway-v2/aws"
version = "0.8.0"
name = "${var.name_prefix}-image-optimizer-${local.name_suffix}"
description = "${var.name_prefix} nextjs image optimizer managed by terraform"
protocol_type = "HTTP"
create_api_domain_name = false
integrations = {
"GET /${var.next_image_path_prefix}/{proxy+}" = {
lambda_arn = module.image_optimizer.this_lambda_function_arn
payload_format_version = "2.0"
timeout_milliseconds = var.lambda_timeout * 1000
}
}
tags = var.tags
}
########################
# CloudFront Integration
########################
locals {
# Query string parameters used by image optimizer
# Must be sorted to prevent unnessesary updates of the cloudFront distribution
cloudfront_allowed_query_string_keys = sort(["url", "w", "q"])
# Headers that are used by the image optimizer
cloudfront_allowed_headers = sort(["accept", "referer"])
cloudfront_origin_image_optimizer = {
domain_name = trimprefix(module.api_gateway.this_apigatewayv2_api_api_endpoint, "https://")
origin_id = var.cloudfront_origin_id
custom_origin_config = {
http_port = "80"
https_port = "443"
origin_protocol_policy = "https-only"
origin_ssl_protocols = ["TLSv1.2"]
}
}
}
resource "aws_cloudfront_origin_request_policy" "this" {
name = "${var.name_prefix}-image-optimizer-request-${local.name_suffix}"
comment = "${var.name_prefix} nextjs image optimizer managed by terraform"
cookies_config {
cookie_behavior = "none"
}
headers_config {
header_behavior = "whitelist"
headers {
items = local.cloudfront_allowed_headers
}
}
query_strings_config {
query_string_behavior = "whitelist"
query_strings {
items = local.cloudfront_allowed_query_string_keys
}
}
}
resource "aws_cloudfront_cache_policy" "this" {
name = "${var.name_prefix}-image-optimizer-cache-${local.name_suffix}"
comment = "${var.name_prefix} nextjs image optimizer managed by terraform"
# 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 = "none"
}
headers_config {
header_behavior = "whitelist"
headers {
items = local.cloudfront_allowed_headers
}
}
query_strings_config {
query_string_behavior = "whitelist"
query_strings {
items = local.cloudfront_allowed_query_string_keys
}
}
enable_accept_encoding_gzip = true
enable_accept_encoding_brotli = true
}
}
module "cloudfront" {
source = "./modules/cloudfront-cache"
cloudfront_create_distribution = var.cloudfront_create_distribution
cloudfront_price_class = var.cloudfront_price_class
cloudfront_origin = local.cloudfront_origin_image_optimizer
cloudfront_origin_request_policy_id = aws_cloudfront_origin_request_policy.this.id
cloudfront_cache_policy_id = aws_cloudfront_cache_policy.this.id
deployment_name = var.deployment_name
tags = var.tags
}