-
Notifications
You must be signed in to change notification settings - Fork 1
/
alb-public-to-k8s.tf
194 lines (171 loc) · 5.91 KB
/
alb-public-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
data "aws_acm_certificate" "acm_certificate" {
count = var.acm_cert_base_domain != "" ? 1 : 0
domain = var.acm_cert_base_domain
types = ["AMAZON_ISSUED"]
most_recent = true
}
resource "aws_lb" "batcave_alb_proxy" {
count = var.create_alb_proxy ? 1 : 0
name = "${var.cluster_name}-alb-proxy"
load_balancer_type = "application"
internal = var.alb_proxy_is_internal
subnets = var.alb_proxy_subnets
security_groups = [aws_security_group.batcave_alb_proxy[0].id]
enable_deletion_protection = var.alb_deletion_protection
drop_invalid_header_fields = var.alb_drop_invalid_header_fields
idle_timeout = var.alb_idle_timeout
access_logs {
bucket = var.logging_bucket
enabled = true
}
tags = merge(
var.tags,
var.alb_public_tags,
)
}
# Listener HTTPS
resource "aws_lb_listener" "batcave_alb_proxy_https" {
count = var.create_alb_proxy ? 1 : 0
load_balancer_arn = aws_lb.batcave_alb_proxy[0].arn
port = "443"
protocol = "HTTPS"
ssl_policy = var.alb_ssl_security_policy
dynamic "default_action" {
for_each = length(var.alb_proxy_restricted_hosts) == 0 ? ["forward all request"] : []
content {
type = "forward"
target_group_arn = aws_lb_target_group.batcave_alb_proxy_https[0].arn
}
}
dynamic "default_action" {
for_each = length(var.alb_proxy_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}_alb_proxy_https"
Environment = var.environment
}
}
# Listener Rule
resource "aws_lb_listener_rule" "batcave_alb__proxy_https" {
for_each = var.alb_proxy_restricted_hosts
listener_arn = aws_lb_listener.batcave_alb_proxy_https[0].arn
action {
type = "forward"
target_group_arn = aws_lb_target_group.batcave_alb_proxy_https[0].arn
}
condition {
host_header {
values = [each.value]
}
}
}
# Redirect from HTTP to HTTPS
resource "aws_lb_listener" "batcave_alb_proxy_http" {
count = var.create_alb_proxy ? 1 : 0
load_balancer_arn = aws_lb.batcave_alb_proxy[0].arn
port = "80"
protocol = "HTTP"
default_action {
type = "redirect"
redirect {
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
tags = {
Name = "${var.cluster_name}_alb_proxy_http"
Environment = var.environment
}
}
# Create HTTPS Target Group
resource "aws_lb_target_group" "batcave_alb_proxy_https" {
count = var.create_alb_proxy ? 1 : 0
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_security_group" "batcave_alb_proxy" {
count = var.create_alb_proxy ? 1 : 0
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_proxy_ingress_cidrs_http" {
count = var.create_alb_proxy && length(var.alb_proxy_ingress_cidrs) > 0 ? 1 : 0
security_group_id = aws_security_group.batcave_alb_proxy[0].id
type = "ingress"
protocol = "tcp"
to_port = 80
from_port = 80
description = "Allow inbound CIDR blocks http"
cidr_blocks = var.alb_proxy_ingress_cidrs
}
resource "aws_security_group_rule" "batcave_alb_proxy_ingress_cidrs_https" {
count = var.create_alb_proxy && length(var.alb_proxy_ingress_cidrs) > 0 ? 1 : 0
security_group_id = aws_security_group.batcave_alb_proxy[0].id
type = "ingress"
protocol = "tcp"
to_port = 443
from_port = 443
description = "Allow inbound CIDR blocks http"
cidr_blocks = var.alb_proxy_ingress_cidrs
}
resource "aws_security_group_rule" "batcave_alb_proxy_ingress_pl_http" {
count = var.create_alb_proxy && length(var.alb_proxy_ingress_prefix_lists) > 0 ? 1 : 0
security_group_id = aws_security_group.batcave_alb_proxy[0].id
type = "ingress"
protocol = "tcp"
to_port = 80
from_port = 80
description = "Allow inbound Prefix Lists http"
prefix_list_ids = var.alb_proxy_ingress_prefix_lists
}
resource "aws_security_group_rule" "batcave_alb_proxy_ingress_pl_https" {
count = var.create_alb_proxy && length(var.alb_proxy_ingress_prefix_lists) > 0 ? 1 : 0
security_group_id = aws_security_group.batcave_alb_proxy[0].id
type = "ingress"
protocol = "tcp"
to_port = 443
from_port = 443
description = "Allow inbound Prefix Lists https"
prefix_list_ids = var.alb_proxy_ingress_prefix_lists
}
resource "aws_security_group_rule" "batcave_alb_proxy_egress" {
count = var.create_alb_proxy ? 1 : 0
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.batcave_alb_proxy[0].id
}
data "aws_wafv2_web_acl" "cms_waf" {
name = "RegSamQuickACLEnforcingV2"
scope = "REGIONAL"
}
resource "aws_wafv2_web_acl_association" "cms_waf_assoc" {
count = var.create_alb_proxy ? 1 : 0
resource_arn = aws_lb.batcave_alb_proxy[0].arn
web_acl_arn = data.aws_wafv2_web_acl.cms_waf.arn
}