-
Notifications
You must be signed in to change notification settings - Fork 28
/
lb.tf
193 lines (157 loc) · 4.41 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" "external_lb" {
count = var.create_extlb ? 1 : 0
name = "${var.common_prefix}-ext-lb-${var.environment}"
load_balancer_type = "network"
internal = "false"
subnets = var.vpc_public_subnets
enable_cross_zone_load_balancing = true
tags = merge(
local.global_tags,
{
"Name" = lower("${var.common_prefix}-ext-lb-${var.environment}")
}
)
}
# HTTP
resource "aws_lb_listener" "external_lb_listener_http" {
count = var.create_extlb ? 1 : 0
load_balancer_arn = aws_lb.external_lb[count.index].arn
protocol = "TCP"
port = var.extlb_http_port
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.external_lb_tg_http[count.index].arn
}
tags = merge(
local.global_tags,
{
"Name" = lower("${var.common_prefix}-http-listener-${var.environment}")
}
)
}
resource "aws_lb_target_group" "external_lb_tg_http" {
count = var.create_extlb ? 1 : 0
port = var.extlb_listener_http_port
protocol = "TCP"
vpc_id = var.vpc_id
proxy_protocol_v2 = true
depends_on = [
aws_lb.external_lb
]
health_check {
protocol = "TCP"
}
lifecycle {
create_before_destroy = true
}
tags = merge(
local.global_tags,
{
"Name" = lower("${var.common_prefix}-ext-lb-tg-http-${var.environment}")
}
)
}
resource "aws_autoscaling_attachment" "target_http" {
count = var.create_extlb ? 1 : 0
depends_on = [
aws_autoscaling_group.k8s_workers_asg,
aws_lb_target_group.external_lb_tg_http
]
autoscaling_group_name = aws_autoscaling_group.k8s_workers_asg.name
lb_target_group_arn = aws_lb_target_group.external_lb_tg_http[count.index].arn
}
# HTTPS
resource "aws_lb_listener" "external_lb_listener_https" {
count = var.create_extlb ? 1 : 0
load_balancer_arn = aws_lb.external_lb[count.index].arn
protocol = "TCP"
port = var.extlb_https_port
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.external_lb_tg_https[count.index].arn
}
tags = merge(
local.global_tags,
{
"Name" = lower("${var.common_prefix}-https-listener-${var.environment}")
}
)
}
resource "aws_lb_target_group" "external_lb_tg_https" {
count = var.create_extlb ? 1 : 0
port = var.extlb_listener_https_port
protocol = "TCP"
vpc_id = var.vpc_id
proxy_protocol_v2 = true
depends_on = [
aws_lb.external_lb
]
health_check {
protocol = "TCP"
}
lifecycle {
create_before_destroy = true
}
tags = merge(
local.global_tags,
{
"Name" = lower("${var.common_prefix}-ext-lb-tg-https-${var.environment}")
}
)
}
resource "aws_autoscaling_attachment" "target_https" {
count = var.create_extlb ? 1 : 0
depends_on = [
aws_autoscaling_group.k8s_workers_asg,
aws_lb_target_group.external_lb_tg_https
]
autoscaling_group_name = aws_autoscaling_group.k8s_workers_asg.name
lb_target_group_arn = aws_lb_target_group.external_lb_tg_https[count.index].arn
}
# kubeapi
resource "aws_lb_listener" "external_lb_listener_kubeapi" {
count = var.expose_kubeapi ? 1 : 0
load_balancer_arn = aws_lb.external_lb[count.index].arn
protocol = "TCP"
port = var.kube_api_port
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.external_lb_tg_kubeapi[count.index].arn
}
tags = merge(
local.global_tags,
{
"Name" = lower("${var.common_prefix}-kubeapi-listener-${var.environment}")
}
)
}
resource "aws_lb_target_group" "external_lb_tg_kubeapi" {
count = var.expose_kubeapi ? 1 : 0
port = var.kube_api_port
protocol = "TCP"
vpc_id = var.vpc_id
depends_on = [
aws_lb.external_lb
]
health_check {
protocol = "TCP"
}
lifecycle {
create_before_destroy = true
}
tags = merge(
local.global_tags,
{
"Name" = lower("${var.common_prefix}-ext-lb-tg-kubeapi-${var.environment}")
}
)
}
resource "aws_autoscaling_attachment" "target_kubeapi" {
count = var.expose_kubeapi ? 1 : 0
depends_on = [
aws_autoscaling_group.k8s_servers_asg,
aws_lb_target_group.external_lb_tg_kubeapi
]
autoscaling_group_name = aws_autoscaling_group.k8s_servers_asg.name
lb_target_group_arn = aws_lb_target_group.external_lb_tg_kubeapi[count.index].arn
}