From 4bc5a30f7aa879959a473c9e445c323af1c0b1af Mon Sep 17 00:00:00 2001 From: Alex Brice Date: Fri, 15 Oct 2021 18:27:46 -0700 Subject: [PATCH 1/6] Added a 'weighted-forward' dynamic action block to use with the https_listener_rules for multiple weighted target_group --- main.tf | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/main.tf b/main.tf index 122a55c..ec01095 100644 --- a/main.tf +++ b/main.tf @@ -247,6 +247,29 @@ resource "aws_lb_listener_rule" "https_listener_rule" { } } + # weighted forward actions + dynamic "action" { + for_each = [ + for action_rule in var.https_listener_rules[count.index].actions : + action_rule + if action_rule.type == "weighted-forward" + ] + + content { + type = "forward" + forward { + dynamic target_group { + for_each = action.value["target_groups"] + + content { + arn = aws_lb_target_group.main[target_group.value["target_group_index"]].id + weight = target_group.value["weight"] + } + } + } + } + } + # Path Pattern condition dynamic "condition" { for_each = [ From 434e1c89500364450b4f27480be1cbe295ee37f1 Mon Sep 17 00:00:00 2001 From: Alex Brice Date: Fri, 15 Oct 2021 19:02:13 -0700 Subject: [PATCH 2/6] Added an example to the alb example --- examples/complete-alb/main.tf | 25 +++++++++++++++++++++++++ main.tf | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/examples/complete-alb/main.tf b/examples/complete-alb/main.tf index c68bbde..d566b98 100644 --- a/examples/complete-alb/main.tf +++ b/examples/complete-alb/main.tf @@ -246,6 +246,31 @@ module "alb" { }] }] }, + { + https_listener_index = 0 + priority = 4 + + actions = [{ + type = "weighted-forward" + target_groups = [ + { + target_group_index = 1 + weight = 2 + }, + { + target_group_index = 0 + weight = 1 + } + ] + }] + + conditions = [{ + query_strings = [{ + key = "weighted" + value = "true" + }] + }] + }, { https_listener_index = 0 priority = 5000 diff --git a/main.tf b/main.tf index ec01095..e0ea60e 100644 --- a/main.tf +++ b/main.tf @@ -262,7 +262,7 @@ resource "aws_lb_listener_rule" "https_listener_rule" { for_each = action.value["target_groups"] content { - arn = aws_lb_target_group.main[target_group.value["target_group_index"]].id + arn = aws_lb_target_group.main[target_group.value["target_group_index"]].id weight = target_group.value["weight"] } } From e93937ef20853bd208ff44558c891020187dd72c Mon Sep 17 00:00:00 2001 From: Alex Brice Date: Wed, 17 Nov 2021 22:02:58 -0800 Subject: [PATCH 3/6] Added stickiness with fallback and updated alb example --- examples/complete-alb/main.tf | 4 ++++ main.tf | 8 ++++++++ variables.tf | 1 - 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/examples/complete-alb/main.tf b/examples/complete-alb/main.tf index d566b98..9165671 100644 --- a/examples/complete-alb/main.tf +++ b/examples/complete-alb/main.tf @@ -262,6 +262,10 @@ module "alb" { weight = 1 } ] + stickiness = { + enabled = true + duration = 3600 + } }] conditions = [{ diff --git a/main.tf b/main.tf index e0ea60e..08ded20 100644 --- a/main.tf +++ b/main.tf @@ -266,6 +266,14 @@ resource "aws_lb_listener_rule" "https_listener_rule" { weight = target_group.value["weight"] } } + dynamic stickiness { + for_each = length(keys(lookup(action.value, "stickiness", {}))) == 0 ? [] : [lookup(action.value, "stickiness", {})] + + content { + enabled = try(stickiness.value["enabled"], false) + duration = try(stickiness.value["duration"], 1) + } + } } } } diff --git a/variables.tf b/variables.tf index 1b8dbb9..d2f407e 100644 --- a/variables.tf +++ b/variables.tf @@ -178,7 +178,6 @@ variable "http_tcp_listeners_tags" { default = {} } - variable "security_groups" { description = "The security groups to attach to the load balancer. e.g. [\"sg-edcd9784\",\"sg-edcd9785\"]" type = list(string) From e8cb06890080d9147e6fc0833dde6360eb3554cd Mon Sep 17 00:00:00 2001 From: Alex Brice Date: Wed, 17 Nov 2021 22:07:43 -0800 Subject: [PATCH 4/6] Add double quotes around dynamic blocks --- main.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.tf b/main.tf index 08ded20..c08fbc1 100644 --- a/main.tf +++ b/main.tf @@ -258,7 +258,7 @@ resource "aws_lb_listener_rule" "https_listener_rule" { content { type = "forward" forward { - dynamic target_group { + dynamic "target_group" { for_each = action.value["target_groups"] content { @@ -266,7 +266,7 @@ resource "aws_lb_listener_rule" "https_listener_rule" { weight = target_group.value["weight"] } } - dynamic stickiness { + dynamic "stickiness" { for_each = length(keys(lookup(action.value, "stickiness", {}))) == 0 ? [] : [lookup(action.value, "stickiness", {})] content { From d0d724dfba8a0495eaa2a98a70d9dad3232873e5 Mon Sep 17 00:00:00 2001 From: Alex Brice Date: Wed, 17 Nov 2021 22:11:50 -0800 Subject: [PATCH 5/6] Might as well line these up --- examples/complete-alb/main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/complete-alb/main.tf b/examples/complete-alb/main.tf index 9165671..401a9c6 100644 --- a/examples/complete-alb/main.tf +++ b/examples/complete-alb/main.tf @@ -248,7 +248,7 @@ module "alb" { }, { https_listener_index = 0 - priority = 4 + priority = 4 actions = [{ type = "weighted-forward" From 210ee1851bb1c4ceb9c98497353b3c5f686b617c Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Fri, 19 Nov 2021 12:38:05 +0100 Subject: [PATCH 6/6] fix: Fixed behavior with stickiness --- main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.tf b/main.tf index c08fbc1..c9ada7d 100644 --- a/main.tf +++ b/main.tf @@ -267,7 +267,7 @@ resource "aws_lb_listener_rule" "https_listener_rule" { } } dynamic "stickiness" { - for_each = length(keys(lookup(action.value, "stickiness", {}))) == 0 ? [] : [lookup(action.value, "stickiness", {})] + for_each = [lookup(action.value, "stickiness", {})] content { enabled = try(stickiness.value["enabled"], false)