Skip to content

Commit 3d79b89

Browse files
authored
feat: make security group optional (#39)
* feat: make security group optional * fix: server lb name * fix: add missing var * fix: add missing variable to example * adds default value * fix naming convention
1 parent f2b7fd2 commit 3d79b89

File tree

10 files changed

+46
-9
lines changed

10 files changed

+46
-9
lines changed

examples/basic-example/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module "this" {
3535
vpc_id = var.vpc_id
3636
ecs_subnets = var.private_subnet_ids
3737

38+
server_lb_name = var.server_lb_name
3839
server_lb_subnets = var.public_subnet_ids
3940
server_security_group_id = var.server_security_group_id
4041
server_lb_certificate_arn = var.lb_certificate_arn

examples/basic-example/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ variable "kms_signing_key_arn" {
4646
type = string
4747
}
4848

49+
variable "server_lb_name" {
50+
type = string
51+
description = "The name of the server load balancer."
52+
default = null
53+
}
54+
4955
variable "lb_certificate_arn" {
5056
type = string
5157
}

main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ module "lb" {
4040

4141
drain_security_group_id = var.drain_security_group_id
4242

43+
4344
server_port = local.server_port
45+
server_lb_name = var.server_lb_name
4446
server_lb_internal = var.server_lb_internal
4547
server_lb_subnets = var.server_lb_subnets
4648
server_lb_certificate_arn = var.server_lb_certificate_arn

modules/lb/locals.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
locals {
2+
load_balancer_security_group_id = var.load_balancer_security_group_id != null ? var.load_balancer_security_group_id : aws_security_group.load_balancer_sg[0].id
3+
}

modules/lb/mqtt_lb.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
resource "aws_lb" "mqtt" {
22
count = var.mqtt_broker_type == "builtin" ? 1 : 0
33
name = "spacelift-mqtt-${var.suffix}"
4-
security_groups = [aws_security_group.load_balancer_sg.id]
4+
security_groups = [local.load_balancer_security_group_id]
55
subnets = var.mqtt_lb_subnets
66
load_balancer_type = "network"
77
internal = var.mqtt_lb_internal

modules/lb/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,8 @@ output "vcs_gateway_target_group_arn" {
4545
output "vcs_gateway_lb_dns" {
4646
value = var.vcs_gateway_service_security_group_id != null ? aws_lb.vcs_gateway[0].dns_name : null
4747
}
48+
49+
output "load_balancer_security_group_id" {
50+
value = local.load_balancer_security_group_id
51+
description = "The security group ID used by the main load balancer (either provided or created)"
52+
}

modules/lb/security.tf

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
resource "aws_security_group" "load_balancer_sg" {
2+
count = var.load_balancer_security_group_id == null ? 1 : 0
3+
24
name = "load_balancer_sg_${var.suffix}"
35
description = "Allow HTTP and HTTPS traffic to the load balancer"
46
vpc_id = var.vpc_id
57
}
68

79
resource "aws_vpc_security_group_egress_rule" "lb_http_towards_server" {
8-
security_group_id = aws_security_group.load_balancer_sg.id
10+
security_group_id = local.load_balancer_security_group_id
911

1012
description = "Allow all traffic to the server"
1113
from_port = var.server_port
@@ -17,7 +19,7 @@ resource "aws_vpc_security_group_egress_rule" "lb_http_towards_server" {
1719
resource "aws_vpc_security_group_egress_rule" "lb_mqtt_towards_server" {
1820
count = var.mqtt_broker_type == "builtin" ? 1 : 0
1921

20-
security_group_id = aws_security_group.load_balancer_sg.id
22+
security_group_id = local.load_balancer_security_group_id
2123

2224
description = "Allow all traffic to the server"
2325
from_port = var.mqtt_port
@@ -27,7 +29,7 @@ resource "aws_vpc_security_group_egress_rule" "lb_mqtt_towards_server" {
2729
}
2830

2931
resource "aws_vpc_security_group_ingress_rule" "tls" {
30-
security_group_id = aws_security_group.load_balancer_sg.id
32+
security_group_id = local.load_balancer_security_group_id
3133

3234
description = "Accept HTTP connections on port 443"
3335
from_port = 443
@@ -39,7 +41,7 @@ resource "aws_vpc_security_group_ingress_rule" "tls" {
3941
resource "aws_vpc_security_group_ingress_rule" "mqtt" {
4042
count = var.mqtt_broker_type == "builtin" ? 1 : 0
4143

42-
security_group_id = aws_security_group.load_balancer_sg.id
44+
security_group_id = local.load_balancer_security_group_id
4345

4446
description = "Accept TLS connections on port 1984 for built in MQTT server"
4547
from_port = var.mqtt_port
@@ -55,7 +57,7 @@ resource "aws_vpc_security_group_ingress_rule" "http_lb_to_server" {
5557
from_port = var.server_port
5658
to_port = var.server_port
5759
ip_protocol = "tcp"
58-
referenced_security_group_id = aws_security_group.load_balancer_sg.id
60+
referenced_security_group_id = local.load_balancer_security_group_id
5961
}
6062

6163
resource "aws_vpc_security_group_ingress_rule" "mqtt_lb_to_server" {
@@ -67,7 +69,7 @@ resource "aws_vpc_security_group_ingress_rule" "mqtt_lb_to_server" {
6769
from_port = var.mqtt_port
6870
to_port = var.mqtt_port
6971
ip_protocol = "tcp"
70-
referenced_security_group_id = aws_security_group.load_balancer_sg.id
72+
referenced_security_group_id = local.load_balancer_security_group_id
7173
}
7274

7375
resource "aws_security_group" "vcs_gateway_lb_sg" {

modules/lb/server_lb.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
resource "aws_lb" "server" {
2-
name = "server-lb-${var.suffix}"
2+
name = coalesce(var.server_lb_name, "server-lb-${var.suffix}")
33
load_balancer_type = "application"
4-
security_groups = [aws_security_group.load_balancer_sg.id]
4+
security_groups = [local.load_balancer_security_group_id]
55
subnets = var.server_lb_subnets
66
internal = var.server_lb_internal
77
}

modules/lb/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ variable "server_port" {
2323
description = "The port the server is listening on."
2424
}
2525

26+
variable "server_lb_name" {
27+
type = string
28+
description = "The name of the server load balancer."
29+
default = null
30+
}
31+
2632
variable "server_lb_subnets" {
2733
type = list(string)
2834
description = "The subnets to deploy the server load balancer in."
@@ -87,3 +93,9 @@ variable "vcs_gateway_certificate_arn" {
8793
type = string
8894
description = "The ARN of the certificate to use for the VCS gateway load balancer."
8995
}
96+
97+
variable "load_balancer_security_group_id" {
98+
type = string
99+
default = null
100+
description = "The security group ID to use for the main load balancer. If not provided, a new security group will be created."
101+
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ variable "vpc_id" {
3535
description = "The VPC ID to deploy the load balancers in."
3636
}
3737

38+
variable "server_lb_name" {
39+
type = string
40+
description = "The name of the server load balancer."
41+
default = null
42+
}
43+
3844
variable "server_lb_internal" {
3945
type = bool
4046
description = "Whether the server load balancer should be internal or internet-facing. It's false (internet-facing) by default."

0 commit comments

Comments
 (0)