Skip to content

Commit e1daa6e

Browse files
committed
added security group to load-balancer
1 parent 704acfd commit e1daa6e

4 files changed

Lines changed: 49 additions & 7 deletions

File tree

terraform/aws/modules/load-balancer/local.tf

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ locals {
44
nlb_dns_name = var.bootstrap_load_balancer ? aws_lb.this[0].dns_name : data.aws_lb.this[0].dns_name
55
nlb_zone_id = var.bootstrap_load_balancer ? aws_lb.this[0].zone_id : data.aws_lb.this[0].zone_id
66

7-
nlb_has_security_groups = var.bootstrap_load_balancer ? false : length(data.aws_lb.this[0].security_groups) > 0
7+
nlb_security_group_ids = var.bootstrap_load_balancer ? aws_lb.this[0].security_groups : data.aws_lb.this[0].security_groups
88

9-
nlb_security_group_ids = local.nlb_has_security_groups ? data.aws_lb.this[0].security_groups : []
109

11-
nlb_listener_for_egress_rules = concat(
10+
nlb_ports_for_egress_rules = concat(
1211
[for l in var.dbx_proxy_listener : { port = l.port, description = "Databricks to NLB to dbx-proxy listener ${l.name}" }],
13-
contains([for l in var.dbx_proxy_listener : l.port], var.dbx_proxy_health_port) ? [] : [{ port = var.dbx_proxy_health_port, description = "Databricks to NLB to dbx-proxy health check" }],
12+
[{ port = var.dbx_proxy_health_port, description = "Databricks to NLB to dbx-proxy health check" }],
1413
)
1514

16-
nlb_sg_egress_rules = local.nlb_has_security_groups ? [
17-
for pair in setproduct(local.nlb_security_group_ids, local.nlb_listener_for_egress_rules, var.subnet_cidrs) : {
15+
nlb_sg_egress_rules = length(local.nlb_security_group_ids) > 0 ? [
16+
for pair in setproduct(local.nlb_security_group_ids, local.nlb_ports_for_egress_rules, var.subnet_cidrs) : {
1817
security_group_id = pair[0]
1918
description = pair[1].description
2019
port = pair[1].port

terraform/aws/modules/load-balancer/nlb.tf

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,52 @@ resource "aws_lb" "this" {
66
load_balancer_type = "network"
77
internal = true
88
subnets = var.subnet_ids
9+
security_groups = [aws_security_group.this[0].id]
910

1011
enable_cross_zone_load_balancing = true
1112
enable_deletion_protection = false
1213

1314
tags = var.tags
1415
}
1516

17+
resource "aws_security_group" "this" {
18+
count = var.bootstrap_load_balancer ? 1 : 0
19+
20+
name = "${var.prefix}-nlb-sg"
21+
description = "Security group for dbx-proxy NLB"
22+
vpc_id = var.vpc_id
23+
24+
# Outbound from NLB on any listener port
25+
dynamic "egress" {
26+
for_each = { for l in var.dbx_proxy_listener : l.name => l }
27+
content {
28+
description = "Databricks to NLB to dbx-proxy listener ${egress.key}"
29+
from_port = egress.value.port
30+
to_port = egress.value.port
31+
protocol = "tcp"
32+
cidr_blocks = var.subnet_cidrs
33+
}
34+
}
35+
36+
# Health check port
37+
egress {
38+
description = "Databricks to NLB to dbx-proxy health check"
39+
from_port = var.dbx_proxy_health_port
40+
to_port = var.dbx_proxy_health_port
41+
protocol = "tcp"
42+
cidr_blocks = var.subnet_cidrs
43+
}
44+
45+
tags = merge(
46+
var.tags,
47+
{
48+
Name = "${var.prefix}-nlb-sg"
49+
},
50+
)
51+
}
52+
1653
resource "aws_vpc_security_group_egress_rule" "this" {
17-
count = local.nlb_has_security_groups ? length(local.nlb_sg_egress_rules) : 0
54+
count = var.bootstrap_load_balancer ? 0 : length(local.nlb_sg_egress_rules)
1855

1956
security_group_id = local.nlb_sg_egress_rules[count.index].security_group_id
2057
from_port = local.nlb_sg_egress_rules[count.index].port

terraform/aws/modules/load-balancer/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ output "nlb_target_group_arns" {
1616
)
1717
}
1818

19+
output "nlb_security_group_ids" {
20+
description = "Security group IDs attached to the NLB (if any)."
21+
value = tolist(local.nlb_security_group_ids)
22+
}
23+
1924
output "vpc_endpoint_service_arn" {
2025
description = "ARN of the VPC endpoint service if created; otherwise null."
2126
value = length(aws_vpc_endpoint_service.this) > 0 ? aws_vpc_endpoint_service.this[0].arn : null

terraform/aws/outputs.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ output "load_balancer" {
1717
nlb_arn = module.load_balancer.nlb_arn
1818
nlb_dns_name = module.load_balancer.nlb_dns_name
1919
nlb_target_group_arns = module.load_balancer.nlb_target_group_arns
20+
nlb_security_group_ids = module.load_balancer.nlb_security_group_ids
2021
vpc_endpoint_service_arn = module.load_balancer.vpc_endpoint_service_arn
2122
vpc_endpoint_service_name = module.load_balancer.vpc_endpoint_service_name
2223
}

0 commit comments

Comments
 (0)