@@ -61,7 +61,7 @@ resource "aws_alb_target_group" "lb_targets" {
6161 target_type = " ip"
6262
6363 lifecycle {
64- replace_triggered_by = [aws_security_group . ecs_sg ]
64+ replace_triggered_by = [aws_security_group . ecs_sg . id ]
6565 }
6666}
6767
@@ -145,7 +145,7 @@ resource "aws_alb_listener" "http_redirect" {
145145}
146146
147147resource "aws_alb_listener" "http_forward" {
148- count = var. aws_ecs_lb_redirect_enable && ! contains (local. aws_ecs_lb_port ,80 ) && ! var. aws_certificate_enabled ? 1 : 0
148+ count = var. aws_ecs_lb_redirect_enable && ! contains (local. aws_ecs_lb_port ,80 ) && ! var. aws_certificate_enabled && ! var . aws_ecs_lb_www_to_apex_redirect ? 1 : 0
149149 load_balancer_arn = aws_alb. ecs_lb [0 ]. id
150150 port = " 80"
151151 protocol = " HTTP"
@@ -161,7 +161,7 @@ resource "aws_alb_listener" "http_forward" {
161161}
162162
163163resource "aws_security_group_rule" "incoming_alb_http" {
164- count = length (aws_alb_listener. http_redirect )
164+ count = length (aws_alb_listener. http_redirect ) + length (aws_alb_listener . http_forward ) + length (aws_alb_listener . http_www_redirect )
165165 type = " ingress"
166166 from_port = 80
167167 to_port = 80
@@ -174,11 +174,11 @@ resource "aws_alb_listener" "https_redirect" {
174174 count = var. aws_ecs_lb_redirect_enable && ! contains (local. aws_ecs_lb_port ,443 ) && var. aws_certificate_enabled ? 1 : 0
175175 # count = var.aws_ecs_lb_redirect_enable && !contains(local.aws_ecs_lb_port,443) ? var.aws_certificates_selected_arn != "" ? 1 : 0 : 0
176176 # count = var.aws_ecs_lb_redirect_enable && var.aws_certificates_selected_arn != "" && !contains(local.aws_ecs_lb_port,443) ? 1 : 0
177- load_balancer_arn = " ${ aws_alb . ecs_lb [0 ]. id } "
177+ load_balancer_arn = aws_alb. ecs_lb [0 ]. id
178178 port = " 443"
179179 protocol = " HTTPS"
180180 certificate_arn = var. aws_certificates_selected_arn
181- ssl_policy = var. aws_certificates_selected_arn != " " ? " ELBSecurityPolicy-TLS13-1-2-2021-06 " : " " # https://docs.aws.amazon.com/elasticloadbalancing/latest/application/create-https-listener.html
181+ ssl_policy = var. aws_certificates_selected_arn != " " ? var . aws_ecs_lb_ssl_policy : " " # https://docs.aws.amazon.com/elasticloadbalancing/latest/application/create-https-listener.html
182182
183183 default_action {
184184 target_group_arn = aws_alb_target_group. lb_targets [0 ]. id
@@ -188,8 +188,12 @@ resource "aws_alb_listener" "https_redirect" {
188188
189189resource "aws_alb_listener_rule" "redirect_based_on_path_for_http" {
190190 for_each = { for idx , path in local . aws_ecs_lb_container_path_redirect : idx => path if length (path) > 0 }
191- listener_arn = var. aws_certificates_selected_arn != " " ? aws_alb_listener. https_redirect [0 ]. arn : aws_alb_listener. http_redirect [0 ]. arn
192-
191+ # listener_arn = var.aws_certificates_selected_arn != "" ? aws_alb_listener.https_redirect[0].arn : aws_alb_listener.http_redirect[0].arn
192+ listener_arn = var. aws_certificate_enabled ? aws_alb_listener. https_redirect [0 ]. arn : (
193+ length (aws_alb_listener. http_redirect ) > 0 ? aws_alb_listener. http_redirect [0 ]. arn : (
194+ length (aws_alb_listener. http_forward ) > 0 ? aws_alb_listener. http_forward [0 ]. arn : aws_alb_listener. http_www_redirect [0 ]. arn
195+ )
196+ )
193197 action {
194198 type = " forward"
195199 target_group_arn = aws_alb_target_group. lb_targets [each . key + 1 ]. arn
@@ -202,9 +206,47 @@ resource "aws_alb_listener_rule" "redirect_based_on_path_for_http" {
202206 }
203207}
204208
209+ resource "aws_alb_listener" "http_www_redirect" {
210+ count = var. aws_ecs_lb_redirect_enable && ! contains (local. aws_ecs_lb_port ,80 ) && ! var. aws_certificate_enabled && var. aws_ecs_lb_www_to_apex_redirect ? 1 : 0
211+ load_balancer_arn = aws_alb. ecs_lb [0 ]. id
212+ port = " 80"
213+ protocol = " HTTP"
214+
215+ default_action {
216+ type = " fixed-response"
217+
218+ fixed_response {
219+ content_type = " text/plain"
220+ message_body = " Not Found"
221+ status_code = " 404"
222+ }
223+ }
224+ depends_on = [
225+ aws_alb . ecs_lb ,
226+ aws_alb_target_group . lb_targets
227+ ]
228+ }
229+
230+ resource "aws_lb_listener_rule" "http_forward_apex" {
231+ count = var. aws_ecs_lb_www_to_apex_redirect && var. aws_r53_domain_name != " " && ! var. aws_certificate_enabled && length (aws_alb_listener. http_www_redirect ) > 0 ? 1 : 0
232+ listener_arn = aws_alb_listener. http_www_redirect [0 ]. arn
233+ priority = 20
234+
235+ condition {
236+ host_header {
237+ values = [var . aws_r53_domain_name ]
238+ }
239+ }
240+
241+ action {
242+ type = " forward"
243+ target_group_arn = aws_alb_target_group. lb_targets [0 ]. id
244+ }
245+ }
246+
205247resource "aws_lb_listener_rule" "redirect_www_to_apex" {
206- count = var. aws_ecs_lb_www_to_apex_redirect && var. aws_r53_domain_name != " " ? 1 : 0
207- listener_arn = var. aws_certificate_enabled ? aws_alb_listener. lb_listener_ssl [0 ]. arn : aws_alb_listener. lb_listener [0 ]. arn
248+ count = var. aws_ecs_lb_www_to_apex_redirect && var. aws_r53_domain_name != " " && (var . aws_certificate_enabled ? length (aws_alb_listener . https_redirect ) > 0 : length (aws_alb_listener . http_www_redirect ) > 0 ) ? 1 : 0
249+ listener_arn = var. aws_certificate_enabled ? aws_alb_listener. https_redirect [0 ]. arn : aws_alb_listener. http_www_redirect [0 ]. arn
208250 priority = 10
209251
210252 condition {
@@ -220,7 +262,7 @@ resource "aws_lb_listener_rule" "redirect_www_to_apex" {
220262 port = var. aws_certificate_enabled ? " 443" : " 80"
221263 protocol = var. aws_certificate_enabled ? " HTTPS" : " HTTP"
222264 status_code = " HTTP_301"
223- host = " ${ var . aws_r53_domain_name } "
265+ host = var. aws_r53_domain_name
224266 path = " /#{path}"
225267 query = " #{query}"
226268 }
0 commit comments