forked from hashicorp/terraform-aws-terraform-enterprise-hvd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_balancer.tf
More file actions
181 lines (149 loc) · 5.37 KB
/
Copy pathload_balancer.tf
File metadata and controls
181 lines (149 loc) · 5.37 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0
#------------------------------------------------------------------------------
# Common
#------------------------------------------------------------------------------
locals {
lb_name_suffix = var.lb_is_internal ? "internal" : "external"
}
#------------------------------------------------------------------------------
# Network load balancer (NLB)
#------------------------------------------------------------------------------
resource "aws_lb" "nlb" {
count = var.lb_type == "nlb" ? 1 : 0
name = "${var.friendly_name_prefix}-tfe-nlb-${local.lb_name_suffix}"
load_balancer_type = "network"
internal = var.lb_is_internal
subnets = var.lb_subnet_ids
security_groups = [
aws_security_group.lb_allow_ingress.id,
aws_security_group.lb_allow_egress.id
]
tags = merge(
{ "Name" = "${var.friendly_name_prefix}-tfe-nlb-${local.lb_name_suffix}" },
var.common_tags
)
}
resource "aws_lb_listener" "lb_nlb_443" {
count = var.lb_type == "nlb" ? 1 : 0
load_balancer_arn = aws_lb.nlb[0].arn
port = 443
protocol = "TCP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.nlb_443[0].arn
}
}
resource "aws_lb_target_group" "nlb_443" {
count = var.lb_type == "nlb" ? 1 : 0
name = "${var.friendly_name_prefix}-tfe-nlb-tg-443"
protocol = "TCP"
port = 443
vpc_id = var.vpc_id
health_check {
protocol = "HTTPS"
path = "/_health_check"
port = "traffic-port"
matcher = "200"
healthy_threshold = 5
unhealthy_threshold = 5
timeout = 10
interval = 30
}
tags = merge(
{ "Name" = "${var.friendly_name_prefix}-tfe-nlb-tg-443" },
{ "Description" = "Load balancer target group for TFE application traffic." },
var.common_tags
)
}
#------------------------------------------------------------------------------
# Application load balancer (ALB)
#------------------------------------------------------------------------------
resource "aws_lb" "alb" {
count = var.lb_type == "alb" ? 1 : 0
name = "${var.friendly_name_prefix}-tfe-alb-${local.lb_name_suffix}"
internal = var.lb_is_internal
load_balancer_type = "application"
subnets = var.lb_subnet_ids
security_groups = [
aws_security_group.lb_allow_ingress.id,
aws_security_group.lb_allow_egress.id
]
tags = merge(
{ "Name" = "${var.friendly_name_prefix}-tfe-alb-${local.lb_name_suffix}" },
var.common_tags
)
}
resource "aws_lb_listener" "alb_443" {
count = var.lb_type == "alb" ? 1 : 0
load_balancer_arn = aws_lb.alb[0].arn
port = 443
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-2016-08"
certificate_arn = var.tfe_alb_tls_certificate_arn
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.alb_443[0].arn
}
}
resource "aws_lb_target_group" "alb_443" {
count = var.lb_type == "alb" ? 1 : 0
name = "${var.friendly_name_prefix}-tfe-alb-tg-443"
port = 443
protocol = "HTTPS"
vpc_id = var.vpc_id
health_check {
protocol = "HTTPS"
path = "/_health_check"
healthy_threshold = 2
unhealthy_threshold = 7
timeout = 5
interval = 30
matcher = 200
}
tags = merge(
{ "Name" = "${var.friendly_name_prefix}-tfe-alb-tg-443" },
{ "Description" = "Load balancer target group for TFE application traffic." },
var.common_tags
)
}
#------------------------------------------------------------------------------
# Security groups
#------------------------------------------------------------------------------
resource "aws_security_group" "lb_allow_ingress" {
name = "${var.friendly_name_prefix}-tfe-lb-allow-ingress"
vpc_id = var.vpc_id
tags = merge({ "Name" = "${var.friendly_name_prefix}-tfe-lb-allow-ingress" }, var.common_tags)
}
resource "aws_security_group_rule" "lb_allow_ingress_tfe_https_from_cidr" {
type = "ingress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = var.cidr_allow_ingress_tfe_443
description = "Allow TCP/443 (HTTPS) inbound to TFE load balancer from specified CIDR ranges."
security_group_id = aws_security_group.lb_allow_ingress.id
}
resource "aws_security_group_rule" "lb_allow_ingress_tfe_https_from_ec2" {
type = "ingress"
from_port = 443
to_port = 443
protocol = "tcp"
source_security_group_id = aws_security_group.ec2_allow_ingress.id
description = "Allow TCP/443 (HTTPS) inbound to TFE load balancer from TFE EC2 security group."
security_group_id = aws_security_group.lb_allow_ingress.id
}
resource "aws_security_group" "lb_allow_egress" {
name = "${var.friendly_name_prefix}-tfe-lb-allow-egress"
vpc_id = var.vpc_id
tags = merge({ "Name" = "${var.friendly_name_prefix}-tfe-lb-allow-egress" }, var.common_tags)
}
resource "aws_security_group_rule" "lb_allow_egress_all" {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow all traffic outbound from the TFE load balancer."
security_group_id = aws_security_group.lb_allow_egress.id
}