Skip to content

Commit dd77e10

Browse files
authored
Redirect HTTP traffic to HTTPS (#925)
- Add HTTP to HTTPS redirect rule with priority 50 (lower than the forward rule) when certificate_arn is not null
1 parent a701a2e commit dd77e10

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

infra/modules/service/load_balancer.tf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,33 @@ resource "aws_lb_listener" "alb_listener_http" {
5656
}
5757
}
5858

59+
resource "aws_lb_listener_rule" "http_to_https_redirect" {
60+
count = var.certificate_arn != null ? 1 : 0
61+
62+
listener_arn = aws_lb_listener.alb_listener_http.arn
63+
priority = 50
64+
65+
action {
66+
type = "redirect"
67+
redirect {
68+
port = "443"
69+
protocol = "HTTPS"
70+
status_code = "HTTP_301"
71+
host = "#{host}"
72+
path = "/#{path}"
73+
query = "#{query}"
74+
}
75+
}
76+
condition {
77+
path_pattern {
78+
values = ["/*"]
79+
}
80+
}
81+
}
82+
5983
resource "aws_lb_listener_rule" "app_http_forward" {
84+
count = var.certificate_arn == null ? 1 : 0
85+
6086
listener_arn = aws_lb_listener.alb_listener_http.arn
6187
priority = 100
6288

0 commit comments

Comments
 (0)