Skip to content

Commit 55a80e2

Browse files
committed
Adding optional internal ALB
1 parent 3029517 commit 55a80e2

File tree

4 files changed

+162
-2
lines changed

4 files changed

+162
-2
lines changed

_outputs.tf

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ output "alb_zone_id" {
1414
value = aws_lb.ecs.*.zone_id
1515
}
1616

17+
output "alb_internal_id" {
18+
value = aws_lb.ecs_internal.*.id
19+
}
20+
21+
output "alb_internal_arn" {
22+
value = aws_lb.ecs_internal.*.arn
23+
}
24+
25+
output "alb_internal_dns_name" {
26+
value = aws_lb.ecs_internal.*.dns_name
27+
}
28+
29+
output "alb_internal_zone_id" {
30+
value = aws_lb.ecs_internal.*.zone_id
31+
}
32+
1733
output "ecs_iam_role_arn" {
1834
value = aws_iam_role.ecs.arn
1935
}
@@ -54,10 +70,18 @@ output "alb_listener_https_arn" {
5470
value = aws_lb_listener.ecs_https.*.arn
5571
}
5672

57-
output "test_traffic_route_listener_arn" {
73+
output "alb_listener_test_traffic_arn" {
5874
value = aws_lb_listener.ecs_test_https.*.arn
5975
}
6076

77+
output "alb_internal_listener_https_arn" {
78+
value = aws_lb_listener.ecs_https_internal.*.arn
79+
}
80+
81+
output "alb_internal_listener_test_traffic_arn" {
82+
value = aws_lb_listener.ecs_test_https_internal.*.arn
83+
}
84+
6185
output "ecs_nodes_secgrp_id" {
6286
value = aws_security_group.ecs_nodes.id
6387
}

_variables.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ variable "vpc_id" {
3232

3333
variable "private_subnet_ids" {
3434
type = list(string)
35-
description = "List of private subnet IDs for ECS instances"
35+
description = "List of private subnet IDs for ECS instances and Internal ALB when enabled"
3636
}
3737

3838
variable "public_subnet_ids" {
@@ -70,6 +70,11 @@ variable "alb_only" {
7070
description = "Whether to deploy only an alb and no cloudFront or not with the cluster"
7171
}
7272

73+
variable "alb_internal" {
74+
default = false
75+
description = "Deploys a second internal ALB for private APIs"
76+
}
77+
7378
variable "asg_min" {
7479
default = 1
7580
description = "Min number of instances for autoscaling group"

alb-internal.tf

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
resource "aws_lb" "ecs_internal" {
2+
count = var.alb_internal ? 1 : 0
3+
4+
load_balancer_type = "application"
5+
internal = true
6+
name = "ecs-${var.name}-internal"
7+
subnets = var.private_subnet_ids
8+
9+
security_groups = [
10+
aws_security_group.alb_internal[0].id,
11+
]
12+
13+
idle_timeout = 400
14+
15+
dynamic "access_logs" {
16+
for_each = compact([var.lb_access_logs_bucket])
17+
18+
content {
19+
bucket = var.lb_access_logs_bucket
20+
prefix = var.lb_access_logs_prefix
21+
enabled = true
22+
}
23+
}
24+
25+
tags = {
26+
Name = "ecs-${var.name}-internal"
27+
}
28+
}
29+
30+
resource "aws_lb_listener" "ecs_https_internal" {
31+
count = var.alb_internal ? 1 : 0
32+
33+
load_balancer_arn = aws_lb.ecs_internal[0].arn
34+
port = "443"
35+
protocol = "HTTPS"
36+
ssl_policy = "ELBSecurityPolicy-2016-08"
37+
certificate_arn = var.certificate_arn
38+
39+
default_action {
40+
type = "forward"
41+
target_group_arn = aws_lb_target_group.ecs_default_https_internal[0].arn
42+
}
43+
}
44+
45+
resource "aws_lb_listener" "ecs_test_https_internal" {
46+
count = var.alb_internal ? 1 : 0
47+
48+
load_balancer_arn = aws_lb.ecs_internal[0].arn
49+
port = "8443"
50+
protocol = "HTTPS"
51+
ssl_policy = "ELBSecurityPolicy-2016-08"
52+
certificate_arn = var.certificate_arn
53+
54+
default_action {
55+
type = "forward"
56+
#target_group_arn = aws_lb_target_group.ecs_replacement_https[0].arn
57+
target_group_arn = aws_lb_target_group.ecs_default_https_internal[0].arn
58+
}
59+
}
60+
61+
# Generate a random string to add it to the name of the Target Group
62+
resource "random_string" "alb_internal_prefix" {
63+
count = var.alb_internal ? 1 : 0
64+
length = 4
65+
upper = false
66+
special = false
67+
}
68+
69+
resource "aws_lb_target_group" "ecs_default_https_internal" {
70+
count = var.alb_internal ? 1 : 0
71+
72+
name = substr("ecs-${var.name}-int-default-https-${random_string.alb_internal_prefix[0].result}", 0, 32)
73+
port = 80
74+
protocol = "HTTP"
75+
vpc_id = var.vpc_id
76+
77+
lifecycle {
78+
create_before_destroy = true
79+
}
80+
}
81+
82+
83+

sg-alb-internal.tf

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
resource "aws_security_group" "alb_internal" {
2+
count = var.alb_internal ? 1 : 0
3+
4+
name = "ecs-${var.name}-lb-internal"
5+
description = "SG for ECS Internal ALB"
6+
vpc_id = var.vpc_id
7+
8+
tags = {
9+
Name = "ecs-${var.name}-lb"
10+
}
11+
}
12+
13+
resource "aws_security_group_rule" "https_from_world_to_alb_internal" {
14+
count = var.alb_internal ? 1 : 0
15+
16+
description = "HTTPS ECS Internal ALB"
17+
type = "ingress"
18+
from_port = 443
19+
to_port = 443
20+
protocol = "tcp"
21+
security_group_id = aws_security_group.alb_internal[0].id
22+
cidr_blocks = ["0.0.0.0/0"]
23+
}
24+
25+
resource "aws_security_group_rule" "https_test_listener_from_world_to_alb_internal" {
26+
count = var.alb_internal ? 1 : 0
27+
28+
description = "HTTPS ECS Internal ALB Test Listener"
29+
type = "ingress"
30+
from_port = 8443
31+
to_port = 8443
32+
protocol = "tcp"
33+
security_group_id = aws_security_group.alb_internal[0].id
34+
cidr_blocks = ["0.0.0.0/0"]
35+
}
36+
37+
38+
resource "aws_security_group_rule" "from_alb_internal_to_ecs_nodes" {
39+
count = var.alb_internal ? 1 : 0
40+
41+
description = "Traffic to ECS Nodes"
42+
type = "egress"
43+
from_port = 0
44+
to_port = 0
45+
protocol = "-1"
46+
security_group_id = aws_security_group.alb_internal[0].id
47+
source_security_group_id = aws_security_group.ecs_nodes.id
48+
}

0 commit comments

Comments
 (0)