-
Notifications
You must be signed in to change notification settings - Fork 1
/
loadbalancer.tf
117 lines (99 loc) · 2.94 KB
/
loadbalancer.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
locals {
# get these values from either input variables, or internal resources if the variables weren't passed
certificate_arn = var.acm_certificate_arn == null ? join("", aws_acm_certificate.keycloak-certificate.*.arn) : var.acm_certificate_arn
lb_log_bucket = var.lb_access_logs_s3_bucket == null ? join("", aws_s3_bucket.keycloak-lb-access-logs.*.id) : var.lb_access_logs_s3_bucket
}
resource "aws_security_group" "keycloak-load-balancer-sg" {
vpc_id = var.vpc_id
name = "keycloak-${var.environment}-load-balancer-sg"
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
tags = var.tags
}
resource "aws_alb" "keycloak-load-balancer" {
name = "keycloak-${var.environment}-alb"
internal = false
load_balancer_type = "application"
subnets = var.public_subnets
security_groups = [aws_security_group.keycloak-load-balancer-sg.id]
# LB access logs
dynamic "access_logs" {
# only include this block if var.lb_enable_access_logs is true
for_each = var.lb_enable_access_logs == true ? toset([1]) : toset([])
content {
bucket = local.lb_log_bucket
prefix = "keycloak-${var.environment}"
enabled = var.lb_enable_access_logs
}
}
tags = var.tags
}
resource "aws_lb_target_group" "keycloak-target-group" {
name = "keycloak-${var.environment}-target-group"
port = 8080
protocol = "HTTP"
target_type = "ip"
vpc_id = var.vpc_id
stickiness {
enabled = true
type = "lb_cookie"
}
health_check {
healthy_threshold = "3"
interval = "300"
protocol = "HTTP"
matcher = "200"
timeout = "3"
path = "/auth/health"
port = 9000
unhealthy_threshold = "2"
}
tags = var.tags
}
resource "aws_lb_listener" "keycloak-listener" {
load_balancer_arn = aws_alb.keycloak-load-balancer.id
port = "443"
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-TLS13-1-2-2021-06"
certificate_arn = local.certificate_arn
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.keycloak-target-group.id
}
tags = var.tags
}
resource "aws_lb_listener_rule" "redirect_to_mbta_com" {
listener_arn = aws_lb_listener.keycloak-listener.arn
priority = 100
action {
type = "redirect"
redirect {
host = "www.mbta.com"
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
path = "/"
query = ""
}
}
condition {
path_pattern {
values = ["/"]
}
}
tags = merge(var.tags, {
Name = "Redirect-to-MBTA-page"
})
}