-
Notifications
You must be signed in to change notification settings - Fork 0
/
lb.tf
193 lines (158 loc) · 4.75 KB
/
lb.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
resource "aws_lb" "lb" {
count = local.create_lb ? 1 : 0
name = local.load_balancer_name
subnets = length(local.nlb_eips) == 0 ? local.subnets : []
security_groups = local.lb_security_groups
idle_timeout = var.idle_timeout
enable_cross_zone_load_balancing = var.enable_cross_zone_load_balancing
internal = local.internal
load_balancer_type = var.load_balancer_type
dynamic "subnet_mapping" {
for_each = toset(local.nlb_eips)
content {
subnet_id = subnet_mapping.value
allocation_id = aws_eip.nlb[subnet_mapping.key].allocation_id
}
}
lifecycle {
ignore_changes = [
subnets,
subnet_mapping
]
}
tags = merge(
local.tags,
{ Name = "${local.load_balancer_name} LB" },
)
}
## HTTP Listeners
resource "aws_lb_listener" "http" {
count = local.only_create_http_listener ? 1 : 0
load_balancer_arn = aws_lb.lb[0].id
port = var.http_port
protocol = "HTTP"
# We 403 by default, unless one of the application rules below is met.
default_action {
type = "fixed-response"
fixed_response {
content_type = "text/plain"
status_code = "403"
}
}
}
resource "aws_lb_listener" "http_redirect" {
count = local.create_https_listeners ? 1 : 0
load_balancer_arn = aws_lb.lb[0].id
port = var.http_port
protocol = "HTTP"
default_action {
type = "redirect"
redirect {
port = var.https_port
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
}
## HTTPS Listener
resource "aws_lb_listener" "https" {
count = local.create_https_listeners ? 1 : 0
load_balancer_arn = aws_lb.lb[0].id
port = var.https_port
protocol = "HTTPS"
certificate_arn = local.acm_arn
ssl_policy = var.alb_ssl_policy
# We 403 by default, unless one of the application rules below is met.
default_action {
type = "fixed-response"
fixed_response {
content_type = "text/plain"
status_code = "403"
}
}
depends_on = [
aws_lb_target_group.target_group
]
}
## NLB Listener
resource "aws_lb_listener" "nlb" {
count = local.create_nlb_listeners ? 1 : 0
load_balancer_arn = aws_lb.lb[0].id
port = var.https_port
protocol = var.nlb_protocol
certificate_arn = local.acm_arn
alpn_policy = var.alpn_policy
ssl_policy = var.nlb_ssl_policy
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.target_group[0].arn
}
depends_on = [
aws_lb_target_group.target_group
]
}
resource "aws_lb_listener" "nlb_tcp" {
count = local.create_nlb_tcp_listeners ? 1 : 0
load_balancer_arn = aws_lb.lb[0].id
port = var.tcp_port
protocol = var.nlb_protocol
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.target_group[0].arn
}
depends_on = [
aws_lb_target_group.target_group
]
}
resource "aws_lb_target_group" "target_group" {
count = local.create_lb ? 1 : 0
name = local.target_group_name
port = var.container_port
protocol = local.container_protocol
vpc_id = local.vpc_id
target_type = "ip"
health_check {
healthy_threshold = var.healthcheck_healthy_threshold
unhealthy_threshold = var.healthcheck_unhealthy_threshold
timeout = local.healthcheck_timeout
path = var.healthcheck_path
protocol = local.healthcheck_protocol
interval = var.healthcheck_interval
matcher = local.healthcheck_matcher
}
tags = merge(local.tags, { "Name" = "${local.target_group_name} target group" })
}
## Application Rule
resource "aws_lb_listener_rule" "http_application_rule" {
count = local.http_application_rule_count
listener_arn = aws_lb_listener.http[0].arn
priority = var.route_priority + count.index
action {
type = "forward"
target_group_arn = aws_lb_target_group.target_group[0].arn
}
condition {
host_header {
values = [element(local.aliases, count.index)]
}
}
}
resource "aws_lb_listener_rule" "https_application_rule" {
count = local.https_application_rule_count
listener_arn = aws_lb_listener.https[0].arn
priority = var.route_priority + count.index
action {
type = "forward"
target_group_arn = aws_lb_target_group.target_group[0].arn
}
condition {
host_header {
values = [element(local.aliases, count.index)]
}
}
}
# EIP for NLB
resource "aws_eip" "nlb" {
for_each = toset(local.nlb_eips)
domain = "vpc"
}