Skip to content

Commit fe71391

Browse files
Added alb module
1 parent 3f23674 commit fe71391

File tree

5 files changed

+157
-60
lines changed

5 files changed

+157
-60
lines changed

example/alb/.terraform.lock.hcl

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/alb/main.tf

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,27 @@ terraform {
99
}
1010
}
1111

12-
1312
module "alb" {
1413
source = "../../modules/alb"
1514

15+
vpc_id = "vpc-123445"
16+
1617
alb = {
1718
name = "arc-poc-alb"
1819
internal = false
20+
subnets = ["subnet-1123", "subnet-1113"]
1921
}
2022

2123
alb_target_group = [{
2224
name = "arc-poc-alb-tg"
2325
port = 80
24-
vpc_id = "vpc-1234"
26+
protocol = "HTTP"
27+
vpc_id = "vpc-123445"
2528
health_check = {
2629
enabled = true
2730
path = "/"
2831
}
2932
}]
3033

31-
listener_rules = {}
34+
listener_rules = []
3235
}

modules/alb/main.tf

Lines changed: 94 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
################################################################################
1+
###################################################################
22
## defaults
3-
################################################################################
3+
###################################################################
44
terraform {
55
required_version = "~> 1.5"
66

@@ -11,32 +11,85 @@ terraform {
1111
}
1212
}
1313
}
14+
provider "aws" {
15+
region = var.region
16+
}
1417

15-
################################################################################
18+
###################################################################
1619
## Load balancer
17-
################################################################################
20+
###################################################################
21+
resource "aws_security_group" "lb_sg" {
22+
name = "${var.alb.name}-sg"
23+
description = "Default security group for internet facing ALB"
24+
vpc_id = var.vpc_id
25+
26+
ingress {
27+
from_port = 80
28+
to_port = 80
29+
protocol = "tcp"
30+
cidr_blocks = ["0.0.0.0/0"]
31+
}
1832

19-
resource "aws_lb" "this" {
20-
count = var.create_alb ? 1 : 0
33+
ingress {
34+
from_port = 443
35+
to_port = 443
36+
protocol = "tcp"
37+
cidr_blocks = ["0.0.0.0/0"]
38+
}
39+
40+
egress {
41+
from_port = 0
42+
to_port = 0
43+
protocol = "-1"
44+
cidr_blocks = ["0.0.0.0/0"]
45+
}
46+
47+
tags = {
48+
Name = "${var.alb.name}-sg"
49+
}
50+
}
51+
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+
}
2163

64+
locals {
65+
alb_subnets = var.create_alb ? [for subnet in data.aws_subnets.public : subnet.id] : []
66+
}
67+
68+
resource "aws_lb" "this" {
2269
name = var.alb.name
2370
internal = var.alb.internal
2471
load_balancer_type = var.alb.load_balancer_type
2572
security_groups = [aws_security_group.lb_sg.id]
26-
subnets = [for subnet in aws_subnet.public : subnet.id]
73+
subnets = var.alb.subnets
2774
idle_timeout = var.alb.idle_timeout
2875
enable_deletion_protection = var.alb.enable_deletion_protection
2976
enable_http2 = var.alb.enable_http2
3077

31-
access_logs {
32-
bucket = var.alb.access_logs.bucket
33-
enabled = var.alb.access_logs.enabled
34-
prefix = var.alb.access_logs.prefix
78+
dynamic "access_logs" {
79+
for_each = var.alb.access_logs != null ? [var.alb.access_logs] : []
80+
81+
content {
82+
bucket = access_logs.value.bucket
83+
enabled = access_logs.value.enabled
84+
prefix = access_logs.value.prefix
85+
}
3586
}
3687
}
3788

3889

90+
###################################################################
3991
## Target Group
92+
###################################################################
4093

4194
resource "aws_lb_target_group" "this" {
4295
for_each = { for tg in var.alb_target_group : tg.name => tg }
@@ -66,7 +119,7 @@ resource "aws_lb_target_group" "this" {
66119
}
67120

68121
dynamic "stickiness" {
69-
for_each = each.value.stickiness != null && each.value.stickiness.enabled ? [each.value.stickiness] : []
122+
for_each = each.value.stickiness != null ? [each.value.stickiness] : []
70123
content {
71124
cookie_duration = stickiness.value.cookie_duration
72125
type = stickiness.value.type
@@ -77,51 +130,63 @@ resource "aws_lb_target_group" "this" {
77130
create_before_destroy = true
78131
}
79132

80-
tags = each.value.tags
133+
tags = each.value.tags
81134
}
82135

83-
# Listener
136+
###################################################################
137+
## Listener
138+
###################################################################
139+
84140
resource "aws_lb_listener" "http" {
85141
load_balancer_arn = aws_lb.this.arn
86142
port = var.alb.port
87143
protocol = var.alb.protocol
88144

89-
certificate_arn = var.alb.certificate_arn
145+
certificate_arn = var.alb.certificate_arn
90146

147+
# Static "default_action" for forward
148+
default_action {
149+
type = "forward"
150+
target_group_arn = aws_lb_target_group.this[var.alb_target_group[0].name].arn
151+
}
152+
153+
# Dynamic "default_action" for variable-driven actions
91154
dynamic "default_action" {
92155
for_each = var.listener_rules
156+
93157
content {
94-
type = each.value.actions[0].type
95-
target_group_arn = lookup(each.value.actions[0], "target_group_arn", null)
158+
type = length(each.value.actions) > 0 ? each.value.actions[0].type : null
159+
target_group_arn = length(each.value.actions) > 0 ? lookup(each.value.actions[0], "target_group_arn", null) : null
96160
}
97161
}
98162
}
99163

100164

165+
101166
resource "aws_lb_listener_rule" "this" {
102167
for_each = var.create_listener_rule ? { for rule in var.listener_rules : "${rule.priority}" => rule } : {}
103168

104169
listener_arn = aws_lb_listener.http.arn
105170
priority = each.value.priority
106171

107172
dynamic "condition" {
108-
for_each = each.value.conditions
109-
content {
110-
dynamic "host_header" {
111-
for_each = each.value.field == "host-header" ? [each.value] : []
112-
content {
113-
values = each.value.values
173+
for_each = each.value.conditions
174+
content {
175+
dynamic "host_header" {
176+
for_each = each.value.field == "host-header" ? [each.value] : []
177+
content {
178+
values = each.value.values
179+
}
114180
}
115-
}
116181

117-
dynamic "path_pattern" {
118-
for_each = each.value.field == "path-pattern" ? [each.value] : []
119-
content {
120-
values = each.value.values
182+
dynamic "path_pattern" {
183+
for_each = each.value.field == "path-pattern" ? [each.value] : []
184+
content {
185+
values = each.value.values
186+
}
121187
}
122188
}
123189
}
124-
}
125190

126191
dynamic "action" {
127192
for_each = each.value.actions

modules/alb/outputs.tf

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
################################################################################
22
## alb
33
################################################################################
4-
output "alb_name" {
4+
/* output "alb_name" {
55
description = "Name of the ALB"
66
value = module.alb.alb_name
77
}
88
99
output "alb_arn" {
1010
description = "ARN to the ALB"
11-
value = module.alb.alb_arn
11+
value = aws_lb.this.alb_arn
1212
}
1313
1414
output "alb_dns_name" {
1515
description = "External DNS name to the ALB"
16-
value = module.alb.alb_dns_name
16+
value = aws_lb.this.alb_dns_name
1717
}
1818
1919
output "alb_zone_id" {
2020
description = "Zone ID of the ALB"
21-
value = module.alb.alb_zone_id
21+
value = aws_lb.this.alb_zone_id
22+
} */
23+
24+
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+
}
29+
30+
output "alb_subnets_debug" {
31+
value = local.alb_subnets
2232
}

modules/alb/variables.tf

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,32 @@ variable "create_alb" {
55
}
66

77
variable "create_listener_rule" {
8-
type = bool
9-
default = false
8+
type = bool
9+
default = false
10+
}
11+
12+
variable "region" {
13+
type = string
14+
default = "us-east-1"
15+
}
16+
17+
variable "vpc_id" {
18+
type = string
19+
description = "VPC in which security group for ALB has to be created"
1020
}
1121

1222
variable "alb" {
1323
type = object({
1424
name = optional(string, null)
25+
port = optional(number)
26+
protocol = optional(string, "HTTP")
1527
internal = optional(bool, false)
1628
load_balancer_type = optional(string, "application")
1729
idle_timeout = optional(number, 60)
1830
enable_deletion_protection = optional(bool, false)
1931
enable_http2 = optional(bool, true)
2032
certificate_arn = optional(string, null)
33+
subnets = list(string)
2134

2235
access_logs = optional(object({
2336
bucket = string
@@ -29,10 +42,11 @@ variable "alb" {
2942
})
3043
}
3144

45+
3246
variable "alb_target_group" {
3347
description = "List of target groups to create"
3448
type = list(object({
35-
name = optional(string, null)
49+
name = optional(string, "target-group")
3650
port = number
3751
protocol = optional(string, null)
3852
protocol_version = optional(string, "HTTP1")
@@ -70,12 +84,14 @@ variable "alb_target_group" {
7084
variable "listener_rules" {
7185
description = "List of listener rules to create"
7286
type = list(object({
73-
listener_arn = string
74-
priority = number
75-
conditions = list(object({
76-
field = string
77-
values = list(string)
87+
# listener_arn = string
88+
priority = number
89+
90+
conditions = list(object({
91+
field = string
92+
values = list(string)
7893
}))
94+
7995
actions = list(object({
8096
type = string
8197
target_group_arn = optional(string)
@@ -88,11 +104,14 @@ variable "listener_rules" {
88104
query = optional(string)
89105
status_code = string
90106
}), null)
107+
91108
fixed_response = optional(object({
92109
content_type = string
93110
message_body = optional(string)
94111
status_code = optional(string)
95112
}), null)
113+
96114
}))
115+
97116
}))
98117
}

0 commit comments

Comments
 (0)