-
Notifications
You must be signed in to change notification settings - Fork 20
/
alb-target-group.tf
149 lines (123 loc) · 3.65 KB
/
alb-target-group.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
resource "aws_lb_listener_rule" "green" {
listener_arn = var.alb_listener_https_arn
action {
type = "forward"
target_group_arn = aws_lb_target_group.green.arn
}
dynamic "condition" {
for_each = length(var.paths) > 0 ? [var.paths] : []
content {
path_pattern { values = toset(condition.value) }
}
}
dynamic "condition" {
for_each = length(var.hostnames) > 0 ? [var.hostnames] : []
content {
host_header { values = toset(condition.value) }
}
}
dynamic "condition" {
for_each = length(var.source_ips) > 0 ? [var.source_ips] : []
content {
source_ip { values = toset(condition.value) }
}
}
dynamic "condition" {
for_each = var.http_header
content {
http_header {
http_header_name = condition.value.name
values = condition.value.values
}
}
}
lifecycle {
ignore_changes = [action[0].target_group_arn]
replace_triggered_by = [aws_lb_target_group.green]
}
priority = try(
aws_lb_listener_rule.path_redirects[length(aws_lb_listener_rule.path_redirects) - 1].priority + 1,
try(
aws_lb_listener_rule.green_auth_oidc[0].priority + 1, var.alb_priority != 0 ? var.alb_priority : null
)
)
tags = merge(var.tags, { "Terraform" = true }, )
}
resource "aws_lb_listener_rule" "redirects" {
count = length(compact(split(",", var.hostname_redirects)))
listener_arn = var.alb_listener_https_arn
action {
type = "redirect"
redirect {
host = var.hostnames[0]
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
condition {
host_header {
values = [element(split(",", var.hostname_redirects), count.index)]
}
}
}
resource "aws_lb_listener_rule" "path_redirects" {
count = length(var.redirects)
listener_arn = var.alb_listener_https_arn
action {
type = "redirect"
redirect {
path = keys(var.redirects)[count.index]
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
condition {
path_pattern {
values = [values(var.redirects)[count.index]]
}
}
priority = try(aws_lb_listener_rule.green_auth_oidc[0].priority + 1,
var.alb_priority != 0 ? var.alb_priority : null
)
tags = merge(
var.tags,
{
"Terraform" = true
},
)
}
# Generate a random string to add it to the name of the Target Group
resource "random_string" "alb_prefix" {
length = 4
upper = false
special = false
}
resource "aws_lb_target_group" "green" {
name = var.compat_keep_target_group_naming ? "${var.cluster_name}-${var.name}-gr" : format("%s-gr-%s", substr("${var.cluster_name}-${replace(var.name, "_", "-")}", 0, 24), random_string.alb_prefix.result)
port = var.port
protocol = var.protocol
vpc_id = var.vpc_id
deregistration_delay = 10
target_type = var.launch_type == "FARGATE" ? "ip" : "instance"
health_check {
path = var.healthcheck_path
interval = var.healthcheck_interval
healthy_threshold = var.healthy_threshold
unhealthy_threshold = var.unhealthy_threshold
timeout = var.healthcheck_timeout
matcher = var.healthcheck_matcher
protocol = var.protocol
}
dynamic "stickiness" {
for_each = var.dynamic_stickiness
iterator = stickiness
content {
cookie_duration = stickiness.value.cookie_duration
cookie_name = stickiness.value.cookie_name
type = stickiness.value.type
}
}
tags = merge(var.tags, { "Terraform" = true }, )
}