Skip to content

Commit

Permalink
Improved alb module
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharth-singh1 committed Dec 6, 2024
1 parent c4d8f29 commit 9293eab
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 31 deletions.
6 changes: 3 additions & 3 deletions example/alb/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ terraform {
module "alb" {
source = "../../modules/alb"

vpc_id = "vpc-123445"
vpc_id = "vpc-12345"

alb = {
name = "arc-poc-alb"
internal = false
subnets = ["subnet-1123", "subnet-1113"]
port = 80
}

alb_target_group = [{
name = "arc-poc-alb-tg"
port = 80
protocol = "HTTP"
vpc_id = "vpc-123445"
vpc_id = "vpc-12345"
health_check = {
enabled = true
path = "/"
Expand Down
14 changes: 14 additions & 0 deletions modules/alb/data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Fetch all subnets in the VPC
data "aws_subnets" "all" {
filter {
name = "vpc-id"
values = [var.vpc_id]
}
}

# Filter subnets with the "Type=public" tag
data "aws_subnet" "public" {
for_each = toset(data.aws_subnets.all.ids)

id = each.value
}
7 changes: 7 additions & 0 deletions modules/alb/locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Collect public subnets in a list
locals {
public_subnets = [
for s in data.aws_subnet.public :
s.id if lookup(s.tags, "Type", "") == "public"
]
}
30 changes: 11 additions & 19 deletions modules/alb/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ provider "aws" {
}

###################################################################
## Load balancer
## Load balancer Security Group
###################################################################
resource "aws_security_group" "lb_sg" {
name = "${var.alb.name}-sg"
Expand Down Expand Up @@ -49,28 +49,15 @@ resource "aws_security_group" "lb_sg" {
}
}


data "aws_subnets" "public" {
filter {
name = "vpc-id"
values = [var.vpc_id]
}

tags = {
Type = "public"
}
}

locals {
alb_subnets = var.create_alb ? [for subnet in data.aws_subnets.public : subnet.id] : []
}

###################################################################
## Application Load balancer
###################################################################
resource "aws_lb" "this" {
name = var.alb.name
internal = var.alb.internal
load_balancer_type = var.alb.load_balancer_type
security_groups = [aws_security_group.lb_sg.id]
subnets = var.alb.subnets
subnets = local.public_subnets
idle_timeout = var.alb.idle_timeout
enable_deletion_protection = var.alb.enable_deletion_protection
enable_http2 = var.alb.enable_http2
Expand Down Expand Up @@ -159,10 +146,13 @@ resource "aws_lb_listener" "http" {
target_group_arn = length(each.value.actions) > 0 ? lookup(each.value.actions[0], "target_group_arn", null) : null
}
}
depends_on = [ aws_lb_target_group.this ]
}



###################################################################
## Listener Rules
###################################################################
resource "aws_lb_listener_rule" "this" {
for_each = var.create_listener_rule ? { for rule in var.listener_rules : "${rule.priority}" => rule } : {}

Expand Down Expand Up @@ -209,4 +199,6 @@ resource "aws_lb_listener_rule" "this" {
}
}
}

depends_on = [ aws_lb_listener.http ]
}
9 changes: 3 additions & 6 deletions modules/alb/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ output "alb_zone_id" {
} */


output "public_subnet_ids" {
value = data.aws_subnets.public
description = "List of IDs of the public subnets in the specified VPC"
}

output "alb_subnets_debug" {
value = local.alb_subnets
# Use the filtered subnets
output "public_subnets" {
value = local.public_subnets
}
2 changes: 0 additions & 2 deletions modules/alb/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ variable "alb" {
enable_deletion_protection = optional(bool, false)
enable_http2 = optional(bool, true)
certificate_arn = optional(string, null)
subnets = list(string)

access_logs = optional(object({
bucket = string
Expand Down Expand Up @@ -84,7 +83,6 @@ variable "alb_target_group" {
variable "listener_rules" {
description = "List of listener rules to create"
type = list(object({
# listener_arn = string
priority = number

conditions = list(object({
Expand Down
3 changes: 2 additions & 1 deletion modules/ecs-cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ resource "aws_ecs_cluster" "this" {
}


########################################################################CloudWatch Log Group
########################################################################
# CloudWatch Log Group
########################################################################
resource "aws_cloudwatch_log_group" "this" {
count = var.create && var.ecs_cluster.create_cloudwatch_log_group ? 1 : 0
Expand Down

0 comments on commit 9293eab

Please sign in to comment.