Skip to content

Commit 2deecca

Browse files
authored
Merge pull request #42 from oozou/feat/support-multiple-listener-and-tg
feat: add support multiple target group and listener rule
2 parents 291f8b2 + 54da272 commit 2deecca

File tree

3 files changed

+100
-53
lines changed

3 files changed

+100
-53
lines changed

locals.tf

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ locals {
2828
ecs_cluster_arn = "arn:aws:ecs:${data.aws_region.this.name}:${data.aws_caller_identity.this.account_id}:cluster/${var.ecs_cluster_name}"
2929

3030
container_attahced_to_alb_keys = [for key, container in var.container : key if try(container.is_attach_to_lb, false) == true]
31-
is_create_target_group = length(local.container_attahced_to_alb_keys) == 1
32-
container_target_group_object = try(var.container[local.container_attahced_to_alb_keys[0]], {})
3331

3432
# KMS
3533
/*| a | b | (a: enable default kms, b: use custom kms)

main.tf

Lines changed: 93 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -124,54 +124,103 @@ resource "aws_cloudwatch_log_group" "this" {
124124
/* Load Balancer */
125125
/* -------------------------------------------------------------------------- */
126126
resource "aws_lb_target_group" "this" {
127-
count = local.is_create_target_group ? 1 : 0
127+
for_each = var.alb_target_group
128128

129-
name = format("%s-tg", substr(local.container_target_group_object.name, 0, min(29, length(local.container_target_group_object.name))))
129+
name = format("%s-tg", substr(format("%s", replace(each.key, "_", "-")), 0, min(29, length(format("%s", replace(each.key, "_", "-"))))))
130130

131-
port = lookup(local.container_target_group_object, "port_mappings", null)[0].container_port
132-
protocol = lookup(local.container_target_group_object, "port_mappings", null)[0].container_port == 443 ? "HTTPS" : "HTTP"
131+
port = lookup(each.value, "port", null)
132+
protocol = lookup(each.value, "protocol", null)
133133
vpc_id = var.vpc_id
134-
target_type = "ip"
134+
target_type = lookup(each.value, "target_type", "ip")
135135
deregistration_delay = var.target_group_deregistration_delay
136136

137137
health_check {
138-
interval = lookup(var.health_check, "interval", null)
139-
path = lookup(var.health_check, "path", null)
140-
timeout = lookup(var.health_check, "timeout", null)
141-
healthy_threshold = lookup(var.health_check, "healthy_threshold", null)
142-
unhealthy_threshold = lookup(var.health_check, "unhealthy_threshold", null)
143-
matcher = lookup(var.health_check, "matcher", null)
138+
enabled = lookup(each.value.health_check, "enabled", null)
139+
interval = lookup(each.value.health_check, "interval", null)
140+
path = lookup(each.value.health_check, "path", null)
141+
timeout = lookup(each.value.health_check, "timeout", null)
142+
healthy_threshold = lookup(each.value.health_check, "healthy_threshold", null)
143+
unhealthy_threshold = lookup(each.value.health_check, "unhealthy_threshold", null)
144+
matcher = lookup(each.value.health_check, "matcher", null)
144145
}
145146

146-
tags = merge(local.tags, { "Name" = format("%s-tg", substr(local.container_target_group_object.name, 0, min(29, length(local.container_target_group_object.name)))) })
147+
dynamic "stickiness" {
148+
for_each = lookup(each.value, "stickiness", null) == null ? [] : [true]
149+
content {
150+
enabled = lookup(each.value.stickiness, "enabled", null)
151+
type = lookup(each.value.stickiness, "type", null)
152+
cookie_name = lookup(each.value.stickiness, "cookie_name", null)
153+
cookie_duration = lookup(each.value.stickiness, "cookie_duration", null)
154+
}
155+
}
156+
157+
tags = merge(local.tags, { "Name" = format("%s-tg", substr(format("%s", replace(each.key, "_", "-")), 0, min(29, length(format("%s", replace(each.key, "_", "-")))))) })
147158
}
159+
148160
/* ------------------------------ Listener Rule ----------------------------- */
149161
resource "aws_lb_listener_rule" "this" {
150-
count = local.is_create_target_group ? 1 : 0
162+
count = length(var.alb_listener_rules)
151163

152164
listener_arn = var.alb_listener_arn
153-
priority = var.alb_priority
165+
priority = lookup(var.alb_listener_rules[count.index], "alb_priority", null)
166+
167+
dynamic "action" {
168+
for_each = [
169+
# for action_rule in var.additional_alb_rule[count.index].actions :
170+
for action_rule in lookup(var.alb_listener_rules[count.index], "actions") :
171+
action_rule
172+
if action_rule.type == "forward"
173+
]
174+
175+
content {
176+
type = action.value["type"]
177+
target_group_arn = aws_lb_target_group.this[lookup(var.alb_listener_rules[count.index], "target_group")].arn
178+
}
179+
}
154180

155-
action {
156-
type = "forward"
157-
target_group_arn = aws_lb_target_group.this[0].arn
181+
# redirect actions
182+
dynamic "action" {
183+
for_each = [
184+
# for action_rule in var.additional_alb_rule[count.index].actions :
185+
for action_rule in lookup(var.alb_listener_rules[count.index], "actions") :
186+
action_rule
187+
if action_rule.type == "redirect"
188+
]
189+
190+
content {
191+
type = action.value["type"]
192+
redirect {
193+
host = lookup(action.value, "host", null)
194+
path = lookup(action.value, "path", null)
195+
port = lookup(action.value, "port", null)
196+
protocol = lookup(action.value, "protocol", null)
197+
query = lookup(action.value, "query", null)
198+
status_code = action.value["status_code"]
199+
}
200+
}
158201
}
159202

160-
condition {
161-
path_pattern {
162-
values = var.alb_paths == [] ? ["*"] : var.alb_paths
203+
# Path Pattern condition
204+
dynamic "condition" {
205+
for_each = length(lookup(var.alb_listener_rules[count.index], "alb_paths", [])) > 0 ? [true] : []
206+
content {
207+
path_pattern {
208+
values = lookup(var.alb_listener_rules[count.index], "alb_paths")
209+
}
163210
}
164211
}
165212

213+
# Host header condition
166214
dynamic "condition" {
167-
for_each = var.alb_host_header == null ? [] : [true]
215+
for_each = lookup(var.alb_listener_rules[count.index], "alb_host_header", null) == null ? [] : [true]
168216
content {
169217
host_header {
170-
values = [var.alb_host_header]
218+
values = [lookup(var.alb_listener_rules[count.index], "alb_host_header")]
171219
}
172220
}
173221
}
174222

223+
# http header condition
175224
dynamic "condition" {
176225
for_each = var.custom_header_token == "" ? [] : [true]
177226
content {
@@ -182,8 +231,27 @@ resource "aws_lb_listener_rule" "this" {
182231
}
183232
}
184233

234+
# Query string condition
235+
dynamic "condition" {
236+
for_each = length(lookup(var.alb_listener_rules[count.index], "alb_query_strings", [])) > 0 ? [true] : []
237+
238+
content {
239+
dynamic "query_string" {
240+
for_each = [
241+
for query_string in var.alb_listener_rules[count.index].alb_query_strings :
242+
query_string
243+
]
244+
content {
245+
key = lookup(query_string.value, "key", null)
246+
value = query_string.value["value"]
247+
}
248+
}
249+
}
250+
}
251+
185252
tags = local.tags
186253
}
254+
187255
/* -------------------------------------------------------------------------- */
188256
/* Secret */
189257
/* -------------------------------------------------------------------------- */
@@ -369,12 +437,12 @@ resource "aws_ecs_service" "this" {
369437
}
370438

371439
dynamic "load_balancer" {
372-
for_each = local.is_create_target_group ? [true] : []
440+
for_each = var.alb_target_group
373441

374442
content {
375-
target_group_arn = aws_lb_target_group.this[0].arn
443+
target_group_arn = aws_lb_target_group.this[load_balancer.key].arn
376444
container_name = local.name
377-
container_port = local.container_target_group_object.port_mappings[0].container_port
445+
container_port = load_balancer.value.port
378446
}
379447
}
380448

variables.tf

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -112,44 +112,25 @@ variable "vpc_id" {
112112
default = ""
113113
}
114114

115-
variable "health_check" {
116-
description = "Health Check Config for the service"
117-
type = map(string)
118-
default = {}
119-
# default = {
120-
# interval = 20
121-
# path = ""
122-
# timeout = 10
123-
# healthy_threshold = 3
124-
# unhealthy_threshold = 3
125-
# matcher = "200,201,204"
126-
# }
127-
}
128115
/* ------------------------------ Listener Rule ----------------------------- */
129116
variable "alb_listener_arn" {
130117
description = "The ALB listener to attach to"
131118
type = string
132119
default = ""
133120
}
134121

135-
variable "alb_host_header" {
136-
description = "Mention host header for api endpoint"
137-
type = string
138-
default = null
122+
variable "alb_target_group" {
123+
description = "Target group for application"
124+
type = any
125+
default = {}
139126
}
140127

141-
variable "alb_paths" {
142-
description = "Mention list Path For ALB routing eg: [\"/\"] or [\"/route1\"]"
143-
type = list(string)
128+
variable "alb_listener_rules" {
129+
description = "Listener rule to add to listener arn"
130+
type = list(any)
144131
default = []
145132
}
146133

147-
variable "alb_priority" {
148-
description = "Priority of ALB rule https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-listeners.html#listener-rules"
149-
type = string
150-
default = "100"
151-
}
152-
153134
variable "custom_header_token" {
154135
description = "[Required] Specify secret value for custom header"
155136
type = string

0 commit comments

Comments
 (0)