Skip to content

Commit 9293eab

Browse files
Improved alb module
1 parent c4d8f29 commit 9293eab

File tree

7 files changed

+40
-31
lines changed

7 files changed

+40
-31
lines changed

example/alb/main.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ terraform {
1212
module "alb" {
1313
source = "../../modules/alb"
1414

15-
vpc_id = "vpc-123445"
15+
vpc_id = "vpc-12345"
1616

1717
alb = {
1818
name = "arc-poc-alb"
1919
internal = false
20-
subnets = ["subnet-1123", "subnet-1113"]
20+
port = 80
2121
}
2222

2323
alb_target_group = [{
2424
name = "arc-poc-alb-tg"
2525
port = 80
2626
protocol = "HTTP"
27-
vpc_id = "vpc-123445"
27+
vpc_id = "vpc-12345"
2828
health_check = {
2929
enabled = true
3030
path = "/"

modules/alb/data.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Fetch all subnets in the VPC
2+
data "aws_subnets" "all" {
3+
filter {
4+
name = "vpc-id"
5+
values = [var.vpc_id]
6+
}
7+
}
8+
9+
# Filter subnets with the "Type=public" tag
10+
data "aws_subnet" "public" {
11+
for_each = toset(data.aws_subnets.all.ids)
12+
13+
id = each.value
14+
}

modules/alb/locals.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Collect public subnets in a list
2+
locals {
3+
public_subnets = [
4+
for s in data.aws_subnet.public :
5+
s.id if lookup(s.tags, "Type", "") == "public"
6+
]
7+
}

modules/alb/main.tf

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ provider "aws" {
1616
}
1717

1818
###################################################################
19-
## Load balancer
19+
## Load balancer Security Group
2020
###################################################################
2121
resource "aws_security_group" "lb_sg" {
2222
name = "${var.alb.name}-sg"
@@ -49,28 +49,15 @@ resource "aws_security_group" "lb_sg" {
4949
}
5050
}
5151

52-
53-
data "aws_subnets" "public" {
54-
filter {
55-
name = "vpc-id"
56-
values = [var.vpc_id]
57-
}
58-
59-
tags = {
60-
Type = "public"
61-
}
62-
}
63-
64-
locals {
65-
alb_subnets = var.create_alb ? [for subnet in data.aws_subnets.public : subnet.id] : []
66-
}
67-
52+
###################################################################
53+
## Application Load balancer
54+
###################################################################
6855
resource "aws_lb" "this" {
6956
name = var.alb.name
7057
internal = var.alb.internal
7158
load_balancer_type = var.alb.load_balancer_type
7259
security_groups = [aws_security_group.lb_sg.id]
73-
subnets = var.alb.subnets
60+
subnets = local.public_subnets
7461
idle_timeout = var.alb.idle_timeout
7562
enable_deletion_protection = var.alb.enable_deletion_protection
7663
enable_http2 = var.alb.enable_http2
@@ -159,10 +146,13 @@ resource "aws_lb_listener" "http" {
159146
target_group_arn = length(each.value.actions) > 0 ? lookup(each.value.actions[0], "target_group_arn", null) : null
160147
}
161148
}
149+
depends_on = [ aws_lb_target_group.this ]
162150
}
163151

164152

165-
153+
###################################################################
154+
## Listener Rules
155+
###################################################################
166156
resource "aws_lb_listener_rule" "this" {
167157
for_each = var.create_listener_rule ? { for rule in var.listener_rules : "${rule.priority}" => rule } : {}
168158

@@ -209,4 +199,6 @@ resource "aws_lb_listener_rule" "this" {
209199
}
210200
}
211201
}
202+
203+
depends_on = [ aws_lb_listener.http ]
212204
}

modules/alb/outputs.tf

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@ output "alb_zone_id" {
2222
} */
2323

2424

25-
output "public_subnet_ids" {
26-
value = data.aws_subnets.public
27-
description = "List of IDs of the public subnets in the specified VPC"
28-
}
2925

30-
output "alb_subnets_debug" {
31-
value = local.alb_subnets
26+
# Use the filtered subnets
27+
output "public_subnets" {
28+
value = local.public_subnets
3229
}

modules/alb/variables.tf

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ variable "alb" {
3030
enable_deletion_protection = optional(bool, false)
3131
enable_http2 = optional(bool, true)
3232
certificate_arn = optional(string, null)
33-
subnets = list(string)
3433

3534
access_logs = optional(object({
3635
bucket = string
@@ -84,7 +83,6 @@ variable "alb_target_group" {
8483
variable "listener_rules" {
8584
description = "List of listener rules to create"
8685
type = list(object({
87-
# listener_arn = string
8886
priority = number
8987

9088
conditions = list(object({

modules/ecs-cluster/main.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ resource "aws_ecs_cluster" "this" {
6767
}
6868

6969

70-
########################################################################CloudWatch Log Group
70+
########################################################################
71+
# CloudWatch Log Group
7172
########################################################################
7273
resource "aws_cloudwatch_log_group" "this" {
7374
count = var.create && var.ecs_cluster.create_cloudwatch_log_group ? 1 : 0

0 commit comments

Comments
 (0)