-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarget_group.tf
57 lines (45 loc) · 1.48 KB
/
target_group.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
resource "aws_lb_target_group" "alb_ip_tg" {
for_each = var.listener_rule_mappings
name = "${var.alb_name}-${var.target_port}-${each.value.description}"
port = var.target_port
protocol = var.target_protocol
target_type = "ip"
vpc_id = var.vpc_id
}
locals {
map_data = flatten([
for key, m in var.listener_rule_mappings : [
for team, ip in m.target_ips : {
env = key
single_ip = ip
}
]
])
}
resource "aws_lb_target_group_attachment" "alb_ip_group_attach" {
for_each = {
for tm in local.map_data : "${tm.env}-${tm.single_ip}" => tm
}
target_group_arn = aws_lb_target_group.alb_ip_tg[each.value.env].arn
port = var.target_port
# If the private IP address is outside of the VPC scope, this value must be set to all
# https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_target_group_attachment#availability_zone
availability_zone = "all"
# For target_type = ip, target_id is the IP address
target_id = each.value.single_ip
}
resource "aws_lb_listener_rule" "listener_rule" {
for_each = var.listener_rule_mappings
listener_arn = aws_lb_listener.alb_listener.arn
#priority = each.value.priority
action {
type = "forward"
//target_group_arn = aws_alb_target_group.alb_ip_tg[each.key].arn
target_group_arn = aws_lb_target_group.alb_ip_tg[each.key].arn
}
condition {
host_header {
values = each.value.hostname_pattern
}
}
}