-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathec2-lb.tf
114 lines (100 loc) · 3.15 KB
/
ec2-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
## Application Load Balancer - Consul Server
resource "aws_lb" "alb" {
name_prefix = "csul-" # 6 character length
load_balancer_type = "application"
security_groups = [aws_security_group.load_balancer.id]
subnets = aws_subnet.public.*.id
idle_timeout = 60
ip_address_type = "dualstack"
tags = merge(
{ "Name" = "${var.main_project_tag}-alb" },
{ "Project" = var.main_project_tag }
)
}
## Target Group
resource "aws_lb_target_group" "alb_targets" {
name_prefix = "csul-"
port = 8500
protocol = "HTTP"
vpc_id = aws_vpc.consul.id
deregistration_delay = 30
target_type = "instance"
# https://www.consul.io/api-docs/health
health_check {
enabled = true
interval = 10
path = "/v1/status/leader" // the consul API health port?
protocol = "HTTP" // switch to HTTPS?
timeout = 5
healthy_threshold = 3
unhealthy_threshold = 3
matcher = "200"
}
tags = merge(
{ "Name" = "${var.main_project_tag}-tg" },
{ "Project" = var.main_project_tag }
)
}
resource "aws_lb_target_group_attachment" "alb_targets_attachment" {
count = var.server_desired_count
target_group_arn = aws_lb_target_group.alb_targets.arn
target_id = aws_instance.consul_server[count.index].id
port = 8500
}
## Default HTTP listener
resource "aws_lb_listener" "alb_http" {
load_balancer_arn = aws_lb.alb.arn
port = 80
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.alb_targets.arn
}
}
## Application Load Balancer - Consul Web Client
resource "aws_lb" "alb_web" {
name_prefix = "csulw-" # 6 character length
load_balancer_type = "application"
security_groups = [aws_security_group.load_balancer.id]
subnets = aws_subnet.public.*.id
idle_timeout = 60
ip_address_type = "dualstack"
tags = merge(
{ "Name" = "${var.main_project_tag}-alb-web" },
{ "Project" = var.main_project_tag }
)
}
## Target Group
resource "aws_lb_target_group" "alb_targets_web" {
name_prefix = "csulw-"
port = 9090
protocol = "HTTP"
vpc_id = aws_vpc.consul.id
deregistration_delay = 30
target_type = "instance"
# https://www.consul.io/api-docs/health
health_check {
enabled = true
interval = 10
path = "/health" // the consul API health port?
protocol = "HTTP" // switch to HTTPS?
timeout = 5
healthy_threshold = 3
unhealthy_threshold = 3
matcher = "200"
}
tags = merge(
{ "Name" = "${var.main_project_tag}-tg-web" },
{ "Project" = var.main_project_tag }
)
}
## Default HTTP listener
resource "aws_lb_listener" "alb_http_web" {
load_balancer_arn = aws_lb.alb_web.arn
port = 80
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.alb_targets_web.arn
}
}