-
Notifications
You must be signed in to change notification settings - Fork 1
/
alb-private-to-k8s.tf
194 lines (175 loc) · 5.54 KB
/
alb-private-to-k8s.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
# create Batcave ALB that forwards traffic to K8s
resource "aws_lb" "batcave_alb" {
name = "${var.cluster_name}-alb"
load_balancer_type = "application"
internal = true
security_groups = [aws_security_group.batcave_alb.id]
enable_deletion_protection = var.alb_deletion_protection
drop_invalid_header_fields = var.alb_drop_invalid_header_fields
idle_timeout = var.alb_idle_timeout
dynamic "subnet_mapping" {
for_each = var.alb_subnets_by_zone
content {
subnet_id = subnet_mapping.value
}
}
access_logs {
bucket = var.logging_bucket
enabled = true
}
tags = merge(
var.tags,
var.alb_private_tags,
)
}
# Listener HTTPS
resource "aws_lb_listener" "batcave_alb_https" {
load_balancer_arn = aws_lb.batcave_alb.arn
port = "443"
protocol = "HTTPS"
ssl_policy = var.alb_ssl_security_policy
dynamic "default_action" {
for_each = length(var.alb_restricted_hosts) == 0 ? ["forward all request"] : []
content {
type = "forward"
target_group_arn = aws_lb_target_group.batcave_alb_https.arn
}
}
dynamic "default_action" {
for_each = length(var.alb_restricted_hosts) > 0 ? ["deny all request"] : []
content {
type = "fixed-response"
fixed_response {
content_type = "text/plain"
message_body = "Unacceptable Host"
status_code = "403"
}
}
}
certificate_arn = data.aws_acm_certificate.acm_certificate[0].arn
tags = {
Name = "${var.cluster_name}-https-tg"
Environment = var.environment
}
}
# Listener Rule
resource "aws_lb_listener_rule" "batcave_alb_https" {
for_each = var.alb_restricted_hosts
listener_arn = aws_lb_listener.batcave_alb_https.arn
action {
type = "forward"
target_group_arn = aws_lb_target_group.batcave_alb_https.arn
}
condition {
host_header {
values = [each.value]
}
}
}
# Redirect from HTTP to HTTPS
resource "aws_lb_listener" "batcave_alb_http" {
load_balancer_arn = aws_lb.batcave_alb.arn
port = "80"
protocol = "HTTP"
default_action {
type = "redirect"
redirect {
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
tags = {
Name = "${var.cluster_name}-http"
Environment = var.environment
}
}
# Create HTTPS Target Group
resource "aws_lb_target_group" "batcave_alb_https" {
name_prefix = substr(var.cluster_name, 0, 6)
port = 30443
protocol = "HTTPS"
vpc_id = var.vpc_id
deregistration_delay = 30
health_check {
healthy_threshold = 2
unhealthy_threshold = 2
matcher = "200"
interval = 30
path = "/healthz/ready"
protocol = "HTTP" # istio's status-port uses http by default
port = "30020"
}
}
resource "aws_lb_target_group" "batcave_alb_http" {
name_prefix = substr(var.cluster_name, 0, 6)
port = 30080
protocol = "HTTP"
vpc_id = var.vpc_id
deregistration_delay = 30
health_check {
healthy_threshold = 2
unhealthy_threshold = 2
matcher = "200"
interval = 30
path = "/healthz/ready"
protocol = "HTTP"
port = "30020"
}
}
# Security Groups
resource "aws_security_group" "batcave_alb" {
description = "${var.cluster_name}-alb-proxy allow inbound"
vpc_id = var.vpc_id
revoke_rules_on_delete = true
}
resource "aws_security_group_rule" "batcave_alb_egress" {
security_group_id = aws_security_group.batcave_alb.id
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
resource "aws_security_group_rule" "batcave_alb_ingress_pl_http" {
security_group_id = aws_security_group.batcave_alb.id
type = "ingress"
protocol = "tcp"
to_port = 80
from_port = 80
description = "Allow inbound Prefix Lists http"
prefix_list_ids = var.cluster_additional_sg_prefix_lists
}
resource "aws_security_group_rule" "batcave_alb_ingress_pl_https" {
security_group_id = aws_security_group.batcave_alb.id
type = "ingress"
protocol = "tcp"
to_port = 443
from_port = 443
description = "Allow inbound Prefix Lists https"
prefix_list_ids = var.cluster_additional_sg_prefix_lists
}
resource "aws_security_group_rule" "batcave_alb_ingress_cidrs_http" {
count = length(var.node_https_ingress_cidr_blocks) > 0 ? 1 : 0
security_group_id = aws_security_group.batcave_alb.id
type = "ingress"
protocol = "tcp"
to_port = 80
from_port = 80
description = "Allow inbound Prefix Lists http"
cidr_blocks = var.node_https_ingress_cidr_blocks
}
resource "aws_security_group_rule" "batcave_alb_ingress_cidrs_https" {
count = length(var.node_https_ingress_cidr_blocks) > 0 ? 1 : 0
security_group_id = aws_security_group.batcave_alb.id
type = "ingress"
protocol = "tcp"
to_port = 443
from_port = 443
description = "Allow inbound Prefix Lists https"
cidr_blocks = var.node_https_ingress_cidr_blocks
}
resource "aws_wafv2_web_acl_association" "cms_waf_priv_assoc" {
resource_arn = aws_lb.batcave_alb.arn
web_acl_arn = data.aws_wafv2_web_acl.cms_waf.arn
}