forked from cn-terraform/terraform-aws-ecs-alb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
404 lines (360 loc) · 14.6 KB
/
main.tf
File metadata and controls
404 lines (360 loc) · 14.6 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#------------------------------------------------------------------------------
# S3 BUCKET - For access logs
#------------------------------------------------------------------------------
data "aws_elb_service_account" "default" {}
module "lb_logs_s3" {
# If we enable S3 Logs for the ALB, but don't provide our own bucket, create one as part of this module
count = var.enable_s3_logs && var.log_bucket_id == null ? 1 : 0
source = "cn-terraform/logs-s3-bucket/aws"
version = "1.0.6"
name_prefix = "${var.name_prefix}-lb"
aws_principals_identifiers = [data.aws_elb_service_account.default.arn]
block_s3_bucket_public_access = var.block_s3_bucket_public_access
enable_s3_bucket_server_side_encryption = var.enable_s3_bucket_server_side_encryption
s3_bucket_server_side_encryption_sse_algorithm = var.s3_bucket_server_side_encryption_sse_algorithm
s3_bucket_server_side_encryption_key = var.s3_bucket_server_side_encryption_key
tags = var.tags
}
#------------------------------------------------------------------------------
# APPLICATION LOAD BALANCER
#------------------------------------------------------------------------------
resource "random_string" "lb_name" {
count = var.use_random_name_for_lb ? 1 : 0
length = 32
numeric = true
special = false
}
resource "aws_lb" "lb" {
name = var.use_random_name_for_lb ? random_string.lb_name[0].result : substr("${var.name_prefix}-lb", 0, 31)
internal = var.internal
load_balancer_type = "application"
drop_invalid_header_fields = var.drop_invalid_header_fields
subnets = var.internal ? var.private_subnets : var.public_subnets
idle_timeout = var.idle_timeout
enable_deletion_protection = var.enable_deletion_protection
enable_cross_zone_load_balancing = var.enable_cross_zone_load_balancing
enable_http2 = var.enable_http2
ip_address_type = var.ip_address_type
security_groups = compact(
concat(var.security_groups, [aws_security_group.lb_access_sg.id]),
)
dynamic "access_logs" {
for_each = var.enable_s3_logs ? [1] : []
content {
bucket = var.log_bucket_id == null ? module.lb_logs_s3[0].s3_bucket_id : var.log_bucket_id
enabled = var.enable_s3_logs
prefix = var.access_logs_prefix
}
}
tags = merge(
var.tags,
{
Name = "${var.name_prefix}-lb"
},
)
}
resource "aws_wafv2_web_acl_association" "waf_association" {
count = var.waf_web_acl_arn != "" ? 1 : 0
resource_arn = aws_lb.lb.arn
web_acl_arn = var.waf_web_acl_arn
}
#------------------------------------------------------------------------------
# ACCESS CONTROL TO APPLICATION LOAD BALANCER
#------------------------------------------------------------------------------
resource "aws_security_group" "lb_access_sg" {
name = "${var.name_prefix}-lb-access-sg"
description = "Controls access to the Load Balancer"
vpc_id = var.vpc_id
egress {
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
tags = merge(
var.tags,
{
Name = "${var.name_prefix}-lb-access-sg"
},
)
}
resource "aws_security_group_rule" "ingress_through_http" {
for_each = var.http_ports
security_group_id = aws_security_group.lb_access_sg.id
type = "ingress"
from_port = each.value.listener_port
to_port = each.value.listener_port
protocol = "tcp"
cidr_blocks = var.http_ingress_cidr_blocks
prefix_list_ids = var.http_ingress_prefix_list_ids
}
resource "aws_security_group_rule" "ingress_through_https" {
for_each = var.https_ports
security_group_id = aws_security_group.lb_access_sg.id
type = "ingress"
from_port = each.value.listener_port
to_port = each.value.listener_port
protocol = "tcp"
cidr_blocks = var.https_ingress_cidr_blocks
prefix_list_ids = var.https_ingress_prefix_list_ids
}
#------------------------------------------------------------------------------
# AWS LOAD BALANCER - Target Groups
#------------------------------------------------------------------------------
resource "null_resource" "lb_http_tgs_config" {
for_each = {
for name, config in var.http_ports : name => config
if config.type == null || config.type == "forward"
}
triggers = {
# Store the md5 of the config so that if anything in `each.value` changes,
# the trigger changes and thus the resource changes.
config_md5 = md5(jsonencode(each.value))
}
}
resource "random_id" "lb_http_tgs_id" {
for_each = {
for name, config in var.http_ports : name => config
if config.type == null || config.type == "forward"
}
byte_length = 2
lifecycle {
# Trigger a replacement whenever the configuration changes.
replace_triggered_by = [
null_resource.lb_http_tgs_config[each.key],
]
}
}
resource "aws_lb_target_group" "lb_http_tgs" {
for_each = {
for name, config in var.http_ports : name => config
if config.type == null || config.type == "forward"
}
name = "${var.name_prefix}-http-${each.value.target_group_port}-${random_id.lb_http_tgs_id[each.key].hex}"
port = each.value.target_group_port
protocol = coalesce(each.value.target_group_protocol, "HTTP")
protocol_version = coalesce(each.value.target_group_protocol_version, "HTTP1")
vpc_id = var.vpc_id
deregistration_delay = var.deregistration_delay
slow_start = var.slow_start
load_balancing_algorithm_type = var.load_balancing_algorithm_type
dynamic "stickiness" {
for_each = var.stickiness == null ? [] : [var.stickiness]
content {
type = stickiness.value.type
cookie_duration = stickiness.value.cookie_duration
enabled = stickiness.value.enabled
}
}
health_check {
enabled = coalesce(each.value.target_group_health_check_enabled, var.target_group_health_check_enabled)
interval = coalesce(each.value.target_group_health_check_interval, var.target_group_health_check_interval)
path = coalesce(each.value.target_group_health_check_path, var.target_group_health_check_path)
port = coalesce(each.value.target_group_health_check_port, var.target_group_health_check_port)
protocol = coalesce(each.value.target_group_health_check_protocol, var.target_group_health_check_protocol)
timeout = coalesce(each.value.target_group_health_check_timeout, var.target_group_health_check_timeout)
healthy_threshold = coalesce(each.value.target_group_health_check_healthy_threshold, var.target_group_health_check_healthy_threshold)
unhealthy_threshold = coalesce(each.value.target_group_health_check_unhealthy_threshold, var.target_group_health_check_unhealthy_threshold)
matcher = coalesce(each.value.target_group_health_check_matcher, var.target_group_health_check_matcher)
}
target_type = "ip"
tags = merge(
var.tags,
{
Name = "${var.name_prefix}-http-${each.value.target_group_port}-${random_id.lb_http_tgs_id[each.key].hex}"
},
)
lifecycle {
// Trigger a replacement whenever the configuration changes.
// Creates a new target group with a new name and deletes the old one once the new one is created.
create_before_destroy = true
}
depends_on = [aws_lb.lb]
}
resource "null_resource" "lb_https_tgs_config" {
for_each = {
for name, config in var.https_ports : name => config
if config.type == null || config.type == "forward"
}
triggers = {
# Store the md5 of the config so that if anything in `each.value` changes,
# the trigger changes and thus the resource changes.
config_md5 = md5(jsonencode(each.value))
}
}
resource "random_id" "lb_https_tgs_id" {
for_each = {
for name, config in var.https_ports : name => config
if config.type == null || config.type == "forward"
}
byte_length = 2
lifecycle {
# Trigger a replacement whenever the configuration changes.
replace_triggered_by = [
null_resource.lb_https_tgs_config[each.key],
]
}
}
resource "aws_lb_target_group" "lb_https_tgs" {
for_each = {
for name, config in var.https_ports : name => config
if config.type == null || config.type == "forward"
}
name = "${var.name_prefix}-https-${each.value.target_group_port}-${random_id.lb_https_tgs_id[each.key].hex}"
port = each.value.target_group_port
protocol = coalesce(each.value.target_group_protocol, "HTTP")
protocol_version = coalesce(each.value.target_group_protocol_version, "HTTP1")
vpc_id = var.vpc_id
deregistration_delay = var.deregistration_delay
slow_start = var.slow_start
load_balancing_algorithm_type = var.load_balancing_algorithm_type
dynamic "stickiness" {
for_each = var.stickiness == null ? [] : [var.stickiness]
content {
type = stickiness.value.type
cookie_duration = stickiness.value.cookie_duration
enabled = stickiness.value.enabled
}
}
health_check {
enabled = coalesce(each.value.target_group_health_check_enabled, var.target_group_health_check_enabled)
interval = coalesce(each.value.target_group_health_check_interval, var.target_group_health_check_interval)
path = coalesce(each.value.target_group_health_check_path, var.target_group_health_check_path)
port = coalesce(each.value.target_group_health_check_port, var.target_group_health_check_port)
protocol = coalesce(each.value.target_group_health_check_protocol, var.target_group_health_check_protocol)
timeout = coalesce(each.value.target_group_health_check_timeout, var.target_group_health_check_timeout)
healthy_threshold = coalesce(each.value.target_group_health_check_healthy_threshold, var.target_group_health_check_healthy_threshold)
unhealthy_threshold = coalesce(each.value.target_group_health_check_unhealthy_threshold, var.target_group_health_check_unhealthy_threshold)
matcher = coalesce(each.value.target_group_health_check_matcher, var.target_group_health_check_matcher)
}
target_type = "ip"
tags = merge(
var.tags,
{
Name = "${var.name_prefix}-https-${each.value.target_group_port}-${random_id.lb_https_tgs_id[each.key].hex}"
},
)
lifecycle {
// Trigger a replacement whenever the configuration changes.
// Creates a new target group with a new name and deletes the old one once the new one is created.
create_before_destroy = true
}
depends_on = [aws_lb.lb]
}
#------------------------------------------------------------------------------
# AWS LOAD BALANCER - Listeners
#------------------------------------------------------------------------------
resource "aws_lb_listener" "lb_http_listeners" {
for_each = var.http_ports
load_balancer_arn = aws_lb.lb.arn
port = each.value.listener_port
protocol = "HTTP"
dynamic "default_action" {
for_each = each.value.type == "redirect" ? [1] : []
content {
type = "redirect"
redirect {
host = each.value.host
path = each.value.path
port = each.value.port
protocol = each.value.protocol
query = each.value.query
status_code = coalesce(each.value.status_code, "HTTP_301")
}
}
}
dynamic "default_action" {
for_each = each.value.type == "fixed-response" ? [1] : []
content {
type = "fixed-response"
fixed_response {
content_type = each.value.content_type
message_body = each.value.message_body
status_code = coalesce(each.value.status_code, "200")
}
}
}
# We fallback to using forward type action if type is not defined
dynamic "default_action" {
for_each = (each.value.type == null || each.value.type == "forward") ? [1] : []
content {
target_group_arn = aws_lb_target_group.lb_http_tgs[each.key].arn
type = "forward"
}
}
lifecycle {
ignore_changes = [
default_action #Can be changed by CodeDeploy when used with Fargate
]
}
tags = var.tags
}
resource "aws_lb_listener" "lb_https_listeners" {
for_each = var.https_ports
load_balancer_arn = aws_lb.lb.arn
port = each.value.listener_port
protocol = "HTTPS"
ssl_policy = var.ssl_policy
certificate_arn = var.default_certificate_arn
dynamic "default_action" {
for_each = each.value.type == "redirect" ? [1] : []
content {
type = "redirect"
redirect {
host = each.value.host
path = each.value.path
port = each.value.port
protocol = each.value.protocol
query = each.value.query
status_code = coalesce(each.value.status_code, "HTTP_301")
}
}
}
dynamic "default_action" {
for_each = each.value.type == "fixed-response" ? [1] : []
content {
type = "fixed-response"
fixed_response {
content_type = each.value.content_type
message_body = each.value.message_body
status_code = coalesce(each.value.status_code, "200")
}
}
}
# We fallback to using forward type action if type is not defined
dynamic "default_action" {
for_each = (each.value.type == null || each.value.type == "forward") ? [1] : []
content {
target_group_arn = aws_lb_target_group.lb_https_tgs[each.key].arn
type = "forward"
}
}
lifecycle {
ignore_changes = [
default_action #Can be changed by CodeDeploy when used with Fargate
]
}
tags = var.tags
}
locals {
list_maps_listener_certificate_arns = flatten([
for cert_arn in var.additional_certificates_arn_for_https_listeners : [
for index, listener in aws_lb_listener.lb_https_listeners : {
name = "listener-${index}-${listener.protocol}-${listener.port}"
listener_arn = listener.arn
certificate_arn = cert_arn
}
]
])
map_listener_certificate_arns = {
for obj in local.list_maps_listener_certificate_arns : obj.name => {
listener_arn = obj.listener_arn,
certificate_arn = obj.certificate_arn
}
}
}
resource "aws_lb_listener_certificate" "additional_certificates_for_https_listeners" {
for_each = local.map_listener_certificate_arns
listener_arn = each.value.listener_arn
certificate_arn = each.value.certificate_arn
}