-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcp.tf
60 lines (54 loc) · 1.38 KB
/
tcp.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
locals {
# Filter services that expose a TCP (non-HTTP) port
tcp_services = {
for service_name, service in local.services:
service_name => service
if service.tcp != null
}
# Distinct TCP load balancers
tcp_load_balancer_names = toset(
values(local.tcp_services)[*].tcp.load_balancer_name,
)
}
data "aws_lb" "tcp" {
for_each = local.tcp_load_balancer_names
name = each.key
}
resource "aws_lb_target_group" "tcp" {
/*
The Target Groups to contain tasks with TCP listeners
*/
for_each = local.tcp_services
vpc_id = local.vpc_id
name = "${local.target_group_names[each.key]}-tcp"
port = coalesce(
each.value.tcp.container_port,
each.value.tcp.port,
)
deregistration_delay = 30
protocol = "TCP"
target_type = each.value.is_fargate ? "ip" : "instance"
health_check {
interval = 30
unhealthy_threshold = 2
healthy_threshold = 2
protocol = "TCP"
port = each.value.is_fargate ? coalesce(
each.value.tcp.container_port,
each.value.tcp.port,
) : "traffic-port"
}
}
resource "aws_lb_listener" "tcp" {
/*
Open port in the NLB pointing to service
*/
for_each = local.tcp_services
load_balancer_arn = data.aws_lb.tcp[each.value.tcp.load_balancer_name].arn
port = each.value.tcp.port
protocol = "TCP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.tcp[each.key].arn
}
}