Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support load balancer stickiness config #16

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,13 @@ resource "aws_security_group" "ecs_service" {
)
}

locals {
lb_listeners_by_arn = { for listener in var.lb_listeners : listener.listener_arn => listener.security_group_id... }
distinct_lb_listeners_by_arn = { for key, val in local.lb_listeners_by_arn : key => val[0] }
}

resource "aws_security_group_rule" "loadbalancer" {
for_each = (var.launch_type == "EXTERNAL"
? {}
: { for lb in var.lb_listeners : lb.listener_arn => lb.security_group_id }
)
for_each = local.distinct_lb_listeners_by_arn

security_group_id = aws_security_group.ecs_service[0].id

Expand All @@ -179,10 +181,7 @@ resource "aws_security_group_rule" "loadbalancer" {
}

resource "aws_security_group_rule" "loadbalancer_to_service" {
for_each = (var.launch_type == "EXTERNAL"
? {}
: { for lb in var.lb_listeners : lb.listener_arn => lb.security_group_id }
)
for_each = local.distinct_lb_listeners_by_arn

security_group_id = each.value

Expand Down Expand Up @@ -238,6 +237,17 @@ resource "aws_lb_target_group" "service" {
}
}

dynamic "stickiness" {
for_each = each.value.lb_stickiness[*]
content {
type = each.value.lb_stickiness.type
enabled = each.value.lb_stickiness.enabled
cookie_duration = each.value.lb_stickiness.cookie_duration
cookie_name = each.value.lb_stickiness.cookie_name
}
}


# NOTE: TF is unable to destroy a target group while a listener is attached,
# therefor we have to create a new one before destroying the old. This also means
# we have to let it have a random name, and then tag it with the desired name.
Expand All @@ -257,8 +267,21 @@ resource "aws_lb_listener_rule" "service" {
listener_arn = each.value.listener_arn

action {
type = "forward"
target_group_arn = aws_lb_target_group.service[each.key].arn
type = "forward"
forward {
target_group {
arn = aws_lb_target_group.service[each.key].arn
}

dynamic "stickiness" {
for_each = each.value.lb_stickiness[*]
content {
enabled = true
duration = stickiness.value.cookie_duration
}
}
}

}

dynamic "condition" {
Expand Down
7 changes: 6 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ variable "lb_listeners" {
type = list(object({
listener_arn = string
security_group_id = string

lb_stickiness = optional(object({
type = string
enabled = optional(bool, null)
cookie_duration = optional(number, null)
cookie_name = optional(string, null)
}))
conditions = list(object({
path_pattern = optional(string)
host_header = optional(any)
Expand Down