-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadbalancer.tf
100 lines (87 loc) · 2.48 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
resource "aws_lb" "app_lb" {
name = "app-lb"
internal = true
load_balancer_type = "application"
security_groups = [aws_security_group.load_balancer_sg.id]
subnets = aws_subnet.pv_subnet[*].id
enable_deletion_protection = false
}
resource "aws_lb_target_group" "frontend_tg" {
name = "frontend-tg"
port = 80
protocol = "HTTP"
vpc_id = aws_vpc.main.id
health_check {
path = "/"
interval = 30
timeout = 5
healthy_threshold = 3
unhealthy_threshold = 2
protocol = "HTTP"
port = 80
}
}
resource "aws_lb_target_group" "backend_tg" {
name = "backend-tg"
port = 80
protocol = "HTTP"
vpc_id = aws_vpc.main.id
health_check {
path = "/"
interval = 30
timeout = 5
healthy_threshold = 3
unhealthy_threshold = 2
protocol = "HTTP"
port = 80
}
}
resource "aws_lb_listener_rule" "frontend_rule" {
listener_arn = aws_lb_listener.app_listener.arn
action {
type = "forward"
target_group_arn = aws_lb_target_group.frontend_tg.arn
}
condition {
host_header {
values = ["frontend.example.com"]
}
}
}
resource "aws_lb_listener_rule" "backend_rule" {
listener_arn = aws_lb_listener.app_listener.arn
action {
type = "forward"
target_group_arn = aws_lb_target_group.backend_tg.arn
}
condition {
host_header {
values = ["backend.example.com"]
}
}
}
resource "aws_lb_listener" "app_listener" {
load_balancer_arn = aws_lb.app_lb.arn
port = "80"
protocol = "HTTP"
default_action {
type = "fixed-response"
fixed_response {
content_type = "text/plain"
message_body = "Unsupported path"
status_code = "404"
}
}
}
resource "aws_lb_target_group_attachment" "frontend_attachment" {
depends_on = [ aws_instance.ec2_instance_private ]
target_group_arn = aws_lb_target_group.frontend_tg.arn
target_id = element(aws_instance.ec2_instance_private.*.id, index(var.ec2_names, "frontend"))
port = 80
}
resource "aws_lb_target_group_attachment" "backend_attachment" {
depends_on = [ aws_instance.ec2_instance_private ]
target_group_arn = aws_lb_target_group.backend_tg.arn
target_id = element(aws_instance.ec2_instance_private.*.id, index(var.ec2_names, "backend"))
port = 80
}