@@ -58,10 +58,18 @@ resource "aws_alb_target_group" "lb_targets" {
5858 }
5959}
6060
61- # Redirect all traffic from the ALB to the target group
61+ # Always exists, acts as a safe dependency wrapper
62+ resource "null_resource" "http_redirect_dep" {
63+ triggers = {
64+ id = (
65+ length (aws_alb_listener. http_redirect ) > 0
66+ ) ? aws_alb_listener.http_redirect[0 ].id : " none"
67+ }
68+ }
69+
6270resource "aws_alb_listener" "lb_listener_ssl" {
6371 count = var. aws_certificate_enabled ? length (local. aws_ecs_lb_port ) : 0
64- load_balancer_arn = " ${ aws_alb . ecs_lb . id } "
72+ load_balancer_arn = aws_alb. ecs_lb . id
6573 port = local. aws_ecs_lb_port [count . index ]
6674 # https://docs.aws.amazon.com/elasticloadbalancing/latest/application/create-https-listener.html
6775 ssl_policy = var. aws_ecs_lb_ssl_policy
@@ -72,26 +80,24 @@ resource "aws_alb_listener" "lb_listener_ssl" {
7280 type = " forward"
7381 }
7482 lifecycle {
75- replace_triggered_by = [ aws_alb_listener . http_redirect ]
83+ replace_triggered_by = [null_resource . http_redirect_dep . id ]
7684 }
7785}
7886
7987resource "aws_alb_listener" "lb_listener" {
8088 count = var. aws_certificate_enabled ? 0 : length (local. aws_ecs_lb_port )
81- load_balancer_arn = " ${ aws_alb . ecs_lb . id } "
89+ load_balancer_arn = aws_alb. ecs_lb . id
8290 port = local. aws_ecs_lb_port [count . index ]
8391 protocol = " HTTP"
8492 default_action {
8593 target_group_arn = aws_alb_target_group. lb_targets [count . index ]. id
8694 type = " forward"
8795 }
8896 lifecycle {
89- replace_triggered_by = [ aws_alb_listener . http_redirect ]
97+ replace_triggered_by = [null_resource . http_redirect_dep . id ]
9098 }
9199}
92100
93-
94-
95101resource "aws_alb_listener_rule" "redirect_based_on_path" {
96102 for_each = { for idx , path in local . aws_ecs_lb_container_path : idx => path if length (path) > 0 }
97103 listener_arn = var. aws_certificate_enabled ? aws_alb_listener. lb_listener_ssl [0 ]. arn : aws_alb_listener. lb_listener [0 ]. arn
0 commit comments