Skip to content

Commit c80650f

Browse files
committed
Revert "Merge pull request #42 from oozou/feat/support-multiple-listener-and-tg"
This reverts commit 2deecca, reversing changes made to 291f8b2.
1 parent 2deecca commit c80650f

File tree

3 files changed

+53
-100
lines changed

3 files changed

+53
-100
lines changed

locals.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ 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]], {})
3133

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

main.tf

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

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

131-
port = lookup(each.value, "port", null)
132-
protocol = lookup(each.value, "protocol", null)
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"
133133
vpc_id = var.vpc_id
134-
target_type = lookup(each.value, "target_type", "ip")
134+
target_type = "ip"
135135
deregistration_delay = var.target_group_deregistration_delay
136136

137137
health_check {
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)
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)
145144
}
146145

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, "_", "-")))))) })
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)))) })
158147
}
159-
160148
/* ------------------------------ Listener Rule ----------------------------- */
161149
resource "aws_lb_listener_rule" "this" {
162-
count = length(var.alb_listener_rules)
150+
count = local.is_create_target_group ? 1 : 0
163151

164152
listener_arn = var.alb_listener_arn
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-
}
153+
priority = var.alb_priority
180154

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-
}
155+
action {
156+
type = "forward"
157+
target_group_arn = aws_lb_target_group.this[0].arn
201158
}
202159

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-
}
160+
condition {
161+
path_pattern {
162+
values = var.alb_paths == [] ? ["*"] : var.alb_paths
210163
}
211164
}
212165

213-
# Host header condition
214166
dynamic "condition" {
215-
for_each = lookup(var.alb_listener_rules[count.index], "alb_host_header", null) == null ? [] : [true]
167+
for_each = var.alb_host_header == null ? [] : [true]
216168
content {
217169
host_header {
218-
values = [lookup(var.alb_listener_rules[count.index], "alb_host_header")]
170+
values = [var.alb_host_header]
219171
}
220172
}
221173
}
222174

223-
# http header condition
224175
dynamic "condition" {
225176
for_each = var.custom_header_token == "" ? [] : [true]
226177
content {
@@ -231,27 +182,8 @@ resource "aws_lb_listener_rule" "this" {
231182
}
232183
}
233184

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-
252185
tags = local.tags
253186
}
254-
255187
/* -------------------------------------------------------------------------- */
256188
/* Secret */
257189
/* -------------------------------------------------------------------------- */
@@ -437,12 +369,12 @@ resource "aws_ecs_service" "this" {
437369
}
438370

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

442374
content {
443-
target_group_arn = aws_lb_target_group.this[load_balancer.key].arn
375+
target_group_arn = aws_lb_target_group.this[0].arn
444376
container_name = local.name
445-
container_port = load_balancer.value.port
377+
container_port = local.container_target_group_object.port_mappings[0].container_port
446378
}
447379
}
448380

variables.tf

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,25 +112,44 @@ 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+
}
115128
/* ------------------------------ Listener Rule ----------------------------- */
116129
variable "alb_listener_arn" {
117130
description = "The ALB listener to attach to"
118131
type = string
119132
default = ""
120133
}
121134

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

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

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+
134153
variable "custom_header_token" {
135154
description = "[Required] Specify secret value for custom header"
136155
type = string

0 commit comments

Comments
 (0)