Skip to content

Commit

Permalink
Implement loadbalancer stickiness (#18)
Browse files Browse the repository at this point in the history
* Add support for activating stickiness in the loadbalancer
  • Loading branch information
tomarnebra authored May 14, 2024
1 parent 33fd088 commit 4602640
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
25 changes: 23 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,16 @@ resource "aws_lb_target_group" "service" {
}
}

dynamic "stickiness" {
for_each = var.lb_stickiness[*]
content {
type = var.lb_stickiness.type
enabled = var.lb_stickiness.enabled
cookie_duration = var.lb_stickiness.cookie_duration
cookie_name = var.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,19 @@ 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 = var.lb_stickiness.enabled ? [1] : []
content {
enabled = true
duration = var.lb_stickiness.cookie_duration
}
}
}
}

dynamic "condition" {
Expand Down
16 changes: 16 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,19 @@ variable "custom_metrics" {
}))
default = []
}

variable "lb_stickiness" {
description = "Bind a user's session to a specific target"
nullable = false
type = object({
type = string
enabled = optional(bool, null)
cookie_duration = optional(number, null)
cookie_name = optional(string, null)
})
default = {
type = "lb_cookie"
enabled = false
cookie_duration = 86400 # 24h in seconds
}
}

0 comments on commit 4602640

Please sign in to comment.