Skip to content

Commit

Permalink
feat: Added private-only traffic feature (#192)
Browse files Browse the repository at this point in the history
Added private only trrafic feature
  • Loading branch information
amanpruthi authored Jun 24, 2024
1 parent c813759 commit 1e75812
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 14 deletions.
30 changes: 17 additions & 13 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -174,27 +174,31 @@ module "app_lb" {

fqdn = local.full_fqdn
extra_fqdn = local.extra_fqdn
allowed_inbound_cidr = var.allowed_inbound_cidr
allowed_inbound_ipv6_cidr = var.allowed_inbound_ipv6_cidr
target_port = local.internal_app_port
allowed_inbound_cidr = var.allowed_inbound_cidr
allowed_inbound_ipv6_cidr = var.allowed_inbound_ipv6_cidr
target_port = local.internal_app_port
network_id = local.network_id
network_private_subnets = local.network_private_subnets
network_public_subnets = local.network_public_subnets
enable_private_only_traffic = var.private_only_traffic
private_endpoint_cidr = var.allowed_private_endpoint_cidr

network_id = local.network_id
network_private_subnets = local.network_private_subnets
network_public_subnets = local.network_public_subnets
}

module "private_link" {
count = length(var.private_link_allowed_account_ids) > 0 ? 1 : 0
source = "./modules/private_link"

namespace = var.namespace
allowed_account_ids = var.private_link_allowed_account_ids
deletion_protection = var.deletion_protection
network_private_subnets = local.network_private_subnets
alb_name = local.lb_name_truncated
vpc_id = local.network_id

namespace = var.namespace
allowed_account_ids = var.private_link_allowed_account_ids
deletion_protection = var.deletion_protection
network_private_subnets = local.network_private_subnets
alb_name = local.lb_name_truncated
vpc_id = local.network_id
enable_private_only_traffic = var.private_only_traffic
nlb_security_group = module.app_lb.nlb_security_group
depends_on = [
module.app_lb,
module.wandb
]
}
Expand Down
51 changes: 51 additions & 0 deletions modules/app_lb/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,36 @@ locals {
https_port = 443
}

resource "aws_security_group" "inbound_private" {
count = var.enable_private_only_traffic ? 1 : 0
name = "${var.namespace}-nlb-inbound"
description = "Allow http(s) inbound traffic from private endpoint to wandb"
vpc_id = var.network_id

dynamic "ingress" {
for_each = var.private_endpoint_cidr
content {
from_port = local.https_port
to_port = local.https_port
protocol = "tcp"
description = "Allow HTTPS (port ${local.https_port}) traffic inbound to W&B LB"
cidr_blocks = [ingress.value]
}
}

dynamic "ingress" {
for_each = var.private_endpoint_cidr
content {
from_port = local.http_port
to_port = local.http_port
protocol = "tcp"
description = "Allow HTTP (port ${local.http_port}) traffic inbound to W&B LB"
cidr_blocks = [ingress.value]
}
}
}


resource "aws_security_group" "inbound" {
name = "${var.namespace}-alb-inbound"
description = "Allow http(s) traffic to wandb"
Expand All @@ -27,6 +57,27 @@ resource "aws_security_group" "inbound" {
}
}

resource "aws_security_group_rule" "alb_http_traffic" {
count = var.enable_private_only_traffic ? 1 : 0
type = "ingress"
from_port = local.http_port
to_port = local.http_port
protocol = "tcp"
security_group_id = aws_security_group.inbound.id
source_security_group_id = aws_security_group.inbound_private[0].id
}

resource "aws_security_group_rule" "alb_https_traffic" {
count = var.enable_private_only_traffic ? 1 : 0
type = "ingress"
from_port = local.https_port
to_port = local.https_port
protocol = "tcp"
security_group_id = aws_security_group.inbound.id
source_security_group_id = aws_security_group.inbound_private[0].id
}


resource "aws_security_group" "outbound" {
name = "${var.namespace}-alb-outbound"
vpc_id = var.network_id
Expand Down
8 changes: 8 additions & 0 deletions modules/app_lb/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ output "lb_arn" {

output "tg_app_arn" {
value = aws_lb_target_group.app.arn
}

output "alb_name" {
value = aws_lb.alb.arn
}

output "nlb_security_group" {
value = var.enable_private_only_traffic? aws_security_group.inbound_private[0].id : null
}
11 changes: 11 additions & 0 deletions modules/app_lb/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,15 @@ variable "network_public_subnets" {
variable "target_port" {
type = number
default = 32543
}


variable "private_endpoint_cidr" {
description = "List of CIDR blocks allowed to access the wandb-server"
type = list(string)
}

variable "enable_private_only_traffic" {
description = "Boolean flag to create sg"
type = bool
}
6 changes: 5 additions & 1 deletion modules/private_link/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
locals {
max_lb_name_length = 32 - length("-nlb")
lb_name_truncated = "${substr(var.namespace, 0, local.max_lb_name_length)}-nlb"
lb_name_truncated = var.enable_private_only_traffic ? "${substr(var.namespace, 0, local.max_lb_name_length)}-private-link-nlb" : "${substr(var.namespace, 0, local.max_lb_name_length)}-nlb"
}

resource "aws_lb" "nlb" {
Expand All @@ -9,6 +9,10 @@ resource "aws_lb" "nlb" {
load_balancer_type = "network"
subnets = var.network_private_subnets
enable_deletion_protection = var.deletion_protection
security_groups = var.enable_private_only_traffic ? [var.nlb_security_group] : []
lifecycle {
create_before_destroy = true
}
}

resource "aws_lb_target_group" "nlb" {
Expand Down
7 changes: 7 additions & 0 deletions modules/private_link/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ variable "vpc_id" {
description = "ID of the VPC to create the VPC Endpoint Service in"
type = string
}

variable "enable_private_only_traffic" {
type = bool
}
variable "nlb_security_group" {
type = string
}
13 changes: 13 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,19 @@ variable "private_link_allowed_account_ids" {
default = []
}

variable "allowed_private_endpoint_cidr" {
description = "Private CIDRs allowed to access wandb-server."
nullable = false
type = list(string)
default = []
}

variable "private_only_traffic" {
description = "Enable private only traffic from customer private network"
type = bool
default = false
}

##########################################
# EKS Cluster #
##########################################
Expand Down

0 comments on commit 1e75812

Please sign in to comment.