forked from terraform-aws-modules/terraform-aws-alb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
675 lines (548 loc) · 25.9 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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
data "aws_partition" "current" {}
locals {
create = var.create && var.putin_khuylo
tags = merge(var.tags, { terraform-aws-modules = "alb" })
}
################################################################################
# Load Balancer
################################################################################
resource "aws_lb" "this" {
count = local.create ? 1 : 0
dynamic "access_logs" {
for_each = length(var.access_logs) > 0 ? [var.access_logs] : []
content {
bucket = access_logs.value.bucket
enabled = try(access_logs.value.enabled, true)
prefix = try(access_logs.value.prefix, null)
}
}
dynamic "connection_logs" {
for_each = length(var.connection_logs) > 0 ? [var.connection_logs] : []
content {
bucket = connection_logs.value.bucket
enabled = try(connection_logs.value.enabled, true)
prefix = try(connection_logs.value.prefix, null)
}
}
customer_owned_ipv4_pool = var.customer_owned_ipv4_pool
desync_mitigation_mode = var.desync_mitigation_mode
dns_record_client_routing_policy = var.dns_record_client_routing_policy
drop_invalid_header_fields = var.drop_invalid_header_fields
enable_cross_zone_load_balancing = var.enable_cross_zone_load_balancing
enable_deletion_protection = var.enable_deletion_protection
enable_http2 = var.enable_http2
enable_tls_version_and_cipher_suite_headers = var.enable_tls_version_and_cipher_suite_headers
enable_waf_fail_open = var.enable_waf_fail_open
enable_xff_client_port = var.enable_xff_client_port
enforce_security_group_inbound_rules_on_private_link_traffic = var.enforce_security_group_inbound_rules_on_private_link_traffic
idle_timeout = var.idle_timeout
internal = var.internal
ip_address_type = var.ip_address_type
load_balancer_type = var.load_balancer_type
name = var.name
name_prefix = var.name_prefix
preserve_host_header = var.preserve_host_header
security_groups = var.create_security_group ? concat([aws_security_group.this[0].id], var.security_groups) : var.security_groups
dynamic "subnet_mapping" {
for_each = var.subnet_mapping
content {
allocation_id = lookup(subnet_mapping.value, "allocation_id", null)
ipv6_address = lookup(subnet_mapping.value, "ipv6_address", null)
private_ipv4_address = lookup(subnet_mapping.value, "private_ipv4_address", null)
subnet_id = subnet_mapping.value.subnet_id
}
}
subnets = var.subnets
tags = local.tags
xff_header_processing_mode = var.xff_header_processing_mode
timeouts {
create = try(var.timeouts.create, null)
update = try(var.timeouts.update, null)
delete = try(var.timeouts.delete, null)
}
lifecycle {
ignore_changes = [
tags["elasticbeanstalk:shared-elb-environment-count"]
]
}
}
################################################################################
# Listener(s)
################################################################################
resource "aws_lb_listener" "this" {
for_each = { for k, v in var.listeners : k => v if local.create }
alpn_policy = try(each.value.alpn_policy, null)
certificate_arn = try(each.value.certificate_arn, null)
dynamic "default_action" {
for_each = try([each.value.authenticate_cognito], [])
content {
authenticate_cognito {
authentication_request_extra_params = try(default_action.value.authentication_request_extra_params, null)
on_unauthenticated_request = try(default_action.value.on_unauthenticated_request, null)
scope = try(default_action.value.scope, null)
session_cookie_name = try(default_action.value.session_cookie_name, null)
session_timeout = try(default_action.value.session_timeout, null)
user_pool_arn = default_action.value.user_pool_arn
user_pool_client_id = default_action.value.user_pool_client_id
user_pool_domain = default_action.value.user_pool_domain
}
order = try(default_action.value.order, null)
type = "authenticate-cognito"
}
}
dynamic "default_action" {
for_each = try([each.value.authenticate_oidc], [])
content {
authenticate_oidc {
authentication_request_extra_params = try(default_action.value.authentication_request_extra_params, null)
authorization_endpoint = default_action.value.authorization_endpoint
client_id = default_action.value.client_id
client_secret = default_action.value.client_secret
issuer = default_action.value.issuer
on_unauthenticated_request = try(default_action.value.on_unauthenticated_request, null)
scope = try(default_action.value.scope, null)
session_cookie_name = try(default_action.value.session_cookie_name, null)
session_timeout = try(default_action.value.session_timeout, null)
token_endpoint = default_action.value.token_endpoint
user_info_endpoint = default_action.value.user_info_endpoint
}
order = try(default_action.value.order, null)
type = "authenticate-oidc"
}
}
dynamic "default_action" {
for_each = try([each.value.fixed_response], [])
content {
fixed_response {
content_type = default_action.value.content_type
message_body = try(default_action.value.message_body, null)
status_code = try(default_action.value.status_code, null)
}
order = try(default_action.value.order, null)
type = "fixed-response"
}
}
dynamic "default_action" {
for_each = try([each.value.forward], [])
content {
order = try(default_action.value.order, null)
target_group_arn = length(try(default_action.value.target_groups, [])) > 0 ? null : try(default_action.value.arn, aws_lb_target_group.this[default_action.value.target_group_key].arn, null)
type = "forward"
}
}
dynamic "default_action" {
for_each = try([each.value.weighted_forward], [])
content {
forward {
dynamic "target_group" {
for_each = try(default_action.value.target_groups, [])
content {
arn = try(target_group.value.arn, aws_lb_target_group.this[target_group.value.target_group_key].arn, null)
weight = try(target_group.value.weight, null)
}
}
dynamic "stickiness" {
for_each = try([default_action.value.stickiness], [])
content {
duration = try(stickiness.value.duration, 60)
enabled = try(stickiness.value.enabled, null)
}
}
}
order = try(default_action.value.order, null)
type = "forward"
}
}
dynamic "default_action" {
for_each = try([each.value.redirect], [])
content {
order = try(default_action.value.order, null)
redirect {
host = try(default_action.value.host, null)
path = try(default_action.value.path, null)
port = try(default_action.value.port, null)
protocol = try(default_action.value.protocol, null)
query = try(default_action.value.query, null)
status_code = default_action.value.status_code
}
type = "redirect"
}
}
dynamic "mutual_authentication" {
for_each = try([each.value.mutual_authentication], [])
content {
mode = mutual_authentication.value.mode
trust_store_arn = try(mutual_authentication.value.trust_store_arn, null)
ignore_client_certificate_expiry = try(mutual_authentication.value.ignore_client_certificate_expiry, null)
}
}
load_balancer_arn = aws_lb.this[0].arn
port = try(each.value.port, var.default_port)
protocol = try(each.value.protocol, var.default_protocol)
ssl_policy = contains(["HTTPS", "TLS"], try(each.value.protocol, var.default_protocol)) ? try(each.value.ssl_policy, "ELBSecurityPolicy-TLS13-1-2-Res-2021-06") : try(each.value.ssl_policy, null)
tags = merge(local.tags, try(each.value.tags, {}))
}
################################################################################
# Listener Rule(s)
################################################################################
locals {
# This allows rules to be specified under the listener definition
listener_rules = flatten([
for listener_key, listener_values in var.listeners : [
for rule_key, rule_values in lookup(listener_values, "rules", {}) :
merge(rule_values, {
listener_key = listener_key
rule_key = rule_key
})
]
])
}
resource "aws_lb_listener_rule" "this" {
for_each = { for v in local.listener_rules : "${v.listener_key}/${v.rule_key}" => v if local.create }
listener_arn = try(each.value.listener_arn, aws_lb_listener.this[each.value.listener_key].arn)
priority = try(each.value.priority, null)
dynamic "action" {
for_each = [for action in each.value.actions : action if action.type == "authenticate-cognito"]
content {
type = "authenticate-cognito"
order = try(action.value.order, null)
authenticate_cognito {
authentication_request_extra_params = try(action.value.authentication_request_extra_params, null)
on_unauthenticated_request = try(action.value.on_unauthenticated_request, null)
scope = try(action.value.scope, null)
session_cookie_name = try(action.value.session_cookie_name, null)
session_timeout = try(action.value.session_timeout, null)
user_pool_arn = action.value.user_pool_arn
user_pool_client_id = action.value.user_pool_client_id
user_pool_domain = action.value.user_pool_domain
}
}
}
dynamic "action" {
for_each = [for action in each.value.actions : action if action.type == "authenticate-oidc"]
content {
type = "authenticate-oidc"
order = try(action.value.order, null)
authenticate_oidc {
authentication_request_extra_params = try(action.value.authentication_request_extra_params, null)
authorization_endpoint = action.value.authorization_endpoint
client_id = action.value.client_id
client_secret = action.value.client_secret
issuer = action.value.issuer
on_unauthenticated_request = try(action.value.on_unauthenticated_request, null)
scope = try(action.value.scope, null)
session_cookie_name = try(action.value.session_cookie_name, null)
session_timeout = try(action.value.session_timeout, null)
token_endpoint = action.value.token_endpoint
user_info_endpoint = action.value.user_info_endpoint
}
}
}
dynamic "action" {
for_each = [for action in each.value.actions : action if action.type == "redirect"]
content {
type = "redirect"
order = try(action.value.order, null)
redirect {
host = try(action.value.host, null)
path = try(action.value.path, null)
port = try(action.value.port, null)
protocol = try(action.value.protocol, null)
query = try(action.value.query, null)
status_code = action.value.status_code
}
}
}
dynamic "action" {
for_each = [for action in each.value.actions : action if action.type == "fixed-response"]
content {
type = "fixed-response"
order = try(action.value.order, null)
fixed_response {
content_type = action.value.content_type
message_body = try(action.value.message_body, null)
status_code = try(action.value.status_code, null)
}
}
}
dynamic "action" {
for_each = [for action in each.value.actions : action if action.type == "forward"]
content {
type = "forward"
order = try(action.value.order, null)
target_group_arn = try(action.value.target_group_arn, aws_lb_target_group.this[action.value.target_group_key].arn, null)
}
}
dynamic "action" {
for_each = [for action in each.value.actions : action if action.type == "weighted-forward"]
content {
type = "forward"
order = try(action.value.order, null)
forward {
dynamic "target_group" {
for_each = try(action.value.target_groups, [])
content {
arn = try(target_group.value.arn, aws_lb_target_group.this[target_group.value.target_group_key].arn)
weight = try(target_group.value.weight, null)
}
}
dynamic "stickiness" {
for_each = try([action.value.stickiness], [])
content {
enabled = try(stickiness.value.enabled, null)
duration = try(stickiness.value.duration, 60)
}
}
}
}
}
dynamic "condition" {
for_each = try(each.value.conditions, [])
content {
dynamic "host_header" {
for_each = try([condition.value.host_header], [])
content {
values = host_header.value.values
}
}
dynamic "http_header" {
for_each = try([condition.value.http_header], [])
content {
http_header_name = http_header.value.http_header_name
values = http_header.value.values
}
}
dynamic "http_request_method" {
for_each = try([condition.value.http_request_method], [])
content {
values = http_request_method.value.values
}
}
dynamic "path_pattern" {
for_each = try([condition.value.path_pattern], [])
content {
values = path_pattern.value.values
}
}
dynamic "query_string" {
for_each = try([condition.value.query_string], [])
content {
key = try(query_string.value.key, null)
value = query_string.value.value
}
}
dynamic "source_ip" {
for_each = try([condition.value.source_ip], [])
content {
values = source_ip.value.values
}
}
}
}
tags = merge(local.tags, try(each.value.tags, {}))
}
################################################################################
# Certificate(s)
################################################################################
locals {
# Take the list of `additional_certificate_arns` from the listener and create
# a map entry that maps each certificate to the listener key. This map of maps
# is then used to create the certificate resources.
additional_certs = merge(values({
for listener_key, listener_values in var.listeners : listener_key =>
{
# This will cause certs to be detached and reattached if certificate_arns
# towards the front of the list are updated/removed. However, we need to have
# unique keys on the resulting map and we can't have computed values (i.e. cert ARN)
# in the key so we are using the array index as part of the key.
for idx, cert_arn in lookup(listener_values, "additional_certificate_arns", []) :
"${listener_key}/${idx}" => {
listener_key = listener_key
certificate_arn = cert_arn
}
} if length(lookup(listener_values, "additional_certificate_arns", [])) > 0
})...)
}
resource "aws_lb_listener_certificate" "this" {
for_each = { for k, v in local.additional_certs : k => v if local.create }
listener_arn = aws_lb_listener.this[each.value.listener_key].arn
certificate_arn = each.value.certificate_arn
}
################################################################################
# Target Group(s)
################################################################################
resource "aws_lb_target_group" "this" {
for_each = { for k, v in var.target_groups : k => v if local.create }
connection_termination = try(each.value.connection_termination, null)
deregistration_delay = try(each.value.deregistration_delay, null)
dynamic "health_check" {
for_each = try([each.value.health_check], [])
content {
enabled = try(health_check.value.enabled, null)
healthy_threshold = try(health_check.value.healthy_threshold, null)
interval = try(health_check.value.interval, null)
matcher = try(health_check.value.matcher, null)
path = try(health_check.value.path, null)
port = try(health_check.value.port, null)
protocol = try(health_check.value.protocol, null)
timeout = try(health_check.value.timeout, null)
unhealthy_threshold = try(health_check.value.unhealthy_threshold, null)
}
}
ip_address_type = try(each.value.ip_address_type, null)
lambda_multi_value_headers_enabled = try(each.value.lambda_multi_value_headers_enabled, null)
load_balancing_algorithm_type = try(each.value.load_balancing_algorithm_type, null)
load_balancing_anomaly_mitigation = try(each.value.load_balancing_anomaly_mitigation, null)
load_balancing_cross_zone_enabled = try(each.value.load_balancing_cross_zone_enabled, null)
name = try(each.value.name, null)
name_prefix = try(each.value.name_prefix, null)
port = try(each.value.target_type, null) == "lambda" ? null : try(each.value.port, var.default_port)
preserve_client_ip = try(each.value.preserve_client_ip, null)
protocol = try(each.value.target_type, null) == "lambda" ? null : try(each.value.protocol, var.default_protocol)
protocol_version = try(each.value.protocol_version, null)
proxy_protocol_v2 = try(each.value.proxy_protocol_v2, null)
slow_start = try(each.value.slow_start, null)
dynamic "stickiness" {
for_each = try([each.value.stickiness], [])
content {
cookie_duration = try(stickiness.value.cookie_duration, null)
cookie_name = try(stickiness.value.cookie_name, null)
enabled = try(stickiness.value.enabled, true)
type = var.load_balancer_type == "network" ? "source_ip" : stickiness.value.type
}
}
dynamic "target_failover" {
for_each = try(each.value.target_failover, [])
content {
on_deregistration = target_failover.value.on_deregistration
on_unhealthy = target_failover.value.on_unhealthy
}
}
dynamic "target_health_state" {
for_each = try([each.value.target_health_state], [])
content {
enable_unhealthy_connection_termination = try(target_health_state.value.enable_unhealthy_connection_termination, true)
}
}
target_type = try(each.value.target_type, null)
vpc_id = try(each.value.vpc_id, var.vpc_id)
tags = merge(local.tags, try(each.value.tags, {}))
lifecycle {
create_before_destroy = true
}
}
################################################################################
# Target Group Attachment
################################################################################
resource "aws_lb_target_group_attachment" "this" {
for_each = { for k, v in var.target_groups : k => v if local.create && lookup(v, "create_attachment", true) }
target_group_arn = aws_lb_target_group.this[each.key].arn
target_id = each.value.target_id
port = try(each.value.target_type, null) == "lambda" ? null : try(each.value.port, var.default_port)
availability_zone = try(each.value.availability_zone, null)
depends_on = [aws_lambda_permission.this]
}
resource "aws_lb_target_group_attachment" "additional" {
for_each = { for k, v in var.additional_target_group_attachments : k => v if local.create }
target_group_arn = aws_lb_target_group.this[each.value.target_group_key].arn
target_id = each.value.target_id
port = try(each.value.target_type, null) == "lambda" ? null : try(each.value.port, var.default_port)
availability_zone = try(each.value.availability_zone, null)
depends_on = [aws_lambda_permission.this]
}
################################################################################
# Lambda Permission
################################################################################
# Filter out the attachments for lambda functions. The ALB target group needs
# permission to forward a request on to # the specified lambda function.
# This filtered list is used to create those permission resources. # To get the
# lambda_function_name, the 6th index is taken from the function ARN format below
# arn:aws:lambda:<region>:<account-id>:function:my-function-name:<version-number>
locals {
lambda_target_groups = {
for k, v in var.target_groups :
(k) => merge(v, { lambda_function_name = split(":", v.target_id)[6] })
if try(v.attach_lambda_permission, false)
}
}
resource "aws_lambda_permission" "this" {
for_each = { for k, v in local.lambda_target_groups : k => v if local.create }
function_name = each.value.lambda_function_name
qualifier = try(each.value.lambda_qualifier, null)
statement_id = try(each.value.lambda_statement_id, "AllowExecutionFromLb")
action = try(each.value.lambda_action, "lambda:InvokeFunction")
principal = try(each.value.lambda_principal, "elasticloadbalancing.${data.aws_partition.current.dns_suffix}")
source_arn = aws_lb_target_group.this[each.key].arn
source_account = try(each.value.lambda_source_account, null)
event_source_token = try(each.value.lambda_event_source_token, null)
}
################################################################################
# Security Group
################################################################################
locals {
create_security_group = local.create && var.create_security_group
security_group_name = try(coalesce(var.security_group_name, var.name, var.name_prefix), "")
}
resource "aws_security_group" "this" {
count = local.create_security_group ? 1 : 0
name = var.security_group_use_name_prefix ? null : local.security_group_name
name_prefix = var.security_group_use_name_prefix ? "${local.security_group_name}-" : null
description = coalesce(var.security_group_description, "Security group for ${local.security_group_name} ${var.load_balancer_type} load balancer")
vpc_id = var.vpc_id
tags = merge(local.tags, var.security_group_tags)
lifecycle {
create_before_destroy = true
}
}
resource "aws_vpc_security_group_egress_rule" "this" {
for_each = { for k, v in var.security_group_egress_rules : k => v if local.create_security_group }
# Required
security_group_id = aws_security_group.this[0].id
ip_protocol = try(each.value.ip_protocol, "tcp")
# Optional
cidr_ipv4 = lookup(each.value, "cidr_ipv4", null)
cidr_ipv6 = lookup(each.value, "cidr_ipv6", null)
description = try(each.value.description, null)
from_port = try(each.value.from_port, null)
prefix_list_id = lookup(each.value, "prefix_list_id", null)
referenced_security_group_id = lookup(each.value, "referenced_security_group_id", null)
to_port = try(each.value.to_port, null)
tags = merge(local.tags, var.security_group_tags, try(each.value.tags, {}))
}
resource "aws_vpc_security_group_ingress_rule" "this" {
for_each = { for k, v in var.security_group_ingress_rules : k => v if local.create_security_group }
# Required
security_group_id = aws_security_group.this[0].id
ip_protocol = try(each.value.ip_protocol, "tcp")
# Optional
cidr_ipv4 = lookup(each.value, "cidr_ipv4", null)
cidr_ipv6 = lookup(each.value, "cidr_ipv6", null)
description = try(each.value.description, null)
from_port = try(each.value.from_port, null)
prefix_list_id = lookup(each.value, "prefix_list_id", null)
referenced_security_group_id = lookup(each.value, "referenced_security_group_id", null)
to_port = try(each.value.to_port, null)
tags = merge(local.tags, var.security_group_tags, try(each.value.tags, {}))
}
################################################################################
# Route53 Record(s)
################################################################################
resource "aws_route53_record" "this" {
for_each = { for k, v in var.route53_records : k => v if var.create }
zone_id = each.value.zone_id
name = try(each.value.name, each.key)
type = each.value.type
alias {
name = aws_lb.this[0].dns_name
zone_id = aws_lb.this[0].zone_id
evaluate_target_health = true
}
}
################################################################################
# WAF
################################################################################
resource "aws_wafv2_web_acl_association" "this" {
count = var.associate_web_acl ? 1 : 0
resource_arn = aws_lb.this[0].arn
web_acl_arn = var.web_acl_arn
}