@@ -45,7 +45,6 @@ resource "aws_lb" "graphdb_alb" {
4545
4646 dynamic "access_logs" {
4747 for_each = var. lb_enable_access_logs ? [1 ] : []
48-
4948 content {
5049 bucket = var. lb_access_logs_bucket_name
5150 enabled = true
@@ -69,6 +68,7 @@ resource "aws_lb_target_group" "graphdb_alb_tg" {
6968 interval = var. lb_health_check_interval
7069 healthy_threshold = var. lb_healthy_threshold
7170 unhealthy_threshold = var. lb_unhealthy_threshold
71+
7272 # Prepend context path only if configured
7373 path = var. lb_context_path != " " ? " ${ var . lb_context_path } ${ var . lb_health_check_path } " : (var. graphdb_node_count > 1 ? var. lb_health_check_path : " /protocol" )
7474 }
@@ -85,40 +85,44 @@ resource "aws_lb_listener" "graphdb_alb_http" {
8585 port = 80
8686 protocol = " HTTP"
8787
88+ # 1) HTTP -> HTTPS redirect (when TLS enabled)
8889 dynamic "default_action" {
89- for_each = var. lb_tls_enabled ? [" redirect" ] : (var. lb_context_path != " " ? [" fixed-response" ] : [" forward" ])
90-
90+ for_each = var. lb_tls_enabled ? [1 ] : []
9191 content {
92- type = default_action. value == " redirect" ? " redirect" : (default_action. value == " fixed-response" ? " fixed-response" : " forward" )
93-
94- dynamic "redirect" {
95- for_each = default_action. value == " redirect" ? [1 ] : []
96- content {
97- port = " 443"
98- protocol = " HTTPS"
99- status_code = " HTTP_301"
100- host = " #{host}"
101- path = " /#{path}"
102- query = " #{query}"
103- }
92+ type = " redirect"
93+ redirect {
94+ port = " 443"
95+ protocol = " HTTPS"
96+ status_code = " HTTP_301"
97+ host = " #{host}"
98+ path = " /#{path}"
99+ query = " #{query}"
104100 }
101+ }
102+ }
105103
106- dynamic "fixed_response" {
107- for_each = default_action. value == " fixed-response" ? [1 ] : []
108- content {
109- content_type = " text/plain"
110- message_body = " Not Found"
111- status_code = " 404"
112- }
104+ # 2) Fixed 404 (when context path is set and TLS is NOT enabled)
105+ dynamic "default_action" {
106+ for_each = (! var. lb_tls_enabled && var. lb_context_path != " " ) ? [1 ] : []
107+ content {
108+ type = " fixed-response"
109+ fixed_response {
110+ content_type = " text/plain"
111+ message_body = " Not Found"
112+ status_code = " 404"
113113 }
114+ }
115+ }
114116
115- dynamic "forward" {
116- for_each = default_action. value == " forward" ? [1 ] : []
117- content {
118- target_group {
119- arn = aws_lb_target_group. graphdb_alb_tg [0 ]. arn
120- weight = 1
121- }
117+ # 3) Forward (when no TLS and no context path)
118+ dynamic "default_action" {
119+ for_each = (! var. lb_tls_enabled && var. lb_context_path == " " ) ? [1 ] : []
120+ content {
121+ type = " forward"
122+ forward {
123+ target_group {
124+ arn = aws_lb_target_group. graphdb_alb_tg [0 ]. arn
125+ weight = 1
122126 }
123127 }
124128 }
@@ -129,22 +133,25 @@ resource "aws_lb_listener" "graphdb_alb_http" {
129133 }
130134}
131135
136+ # -------------------------
137+ # HTTP (no TLS) context path rule
138+ # -------------------------
132139resource "aws_lb_listener_rule" "graphdb_path_based_http" {
133140 count = local. is_alb && ! var. lb_tls_enabled && var. lb_context_path != " " ? 1 : 0
134141
135142 listener_arn = aws_lb_listener. graphdb_alb_http [0 ]. arn
136143 priority = 100
137144
138- dynamic "transform" {
139- for_each = var. lb_enable_context_path_rewrite ? [1 ] : []
140- content {
141- type = " url-rewrite"
145+ transform {
146+ type = " url-rewrite"
142147
143- url_rewrite {
144- rewrites = [
145- { regex = " ^${ var . lb_context_path } /(.*)$" , replace = " /$1" },
146- { regex = " ^${ var . lb_context_path } $" , replace = " /" }
147- ]
148+ url_rewrite_config {
149+ rewrite {
150+ # /graphdb
151+ # /graphdb/
152+ # /graphdb/anything
153+ regex = " ^${ var . lb_context_path } (/(.*))?$"
154+ replace = " /$2"
148155 }
149156 }
150157 }
@@ -161,6 +168,78 @@ resource "aws_lb_listener_rule" "graphdb_path_based_http" {
161168 }
162169}
163170
171+ resource "aws_lb_listener_rule" "graphdb_root_redirect_http" {
172+ count = local. is_alb && ! var. lb_tls_enabled && var. lb_context_path != " " ? 1 : 0
173+
174+ listener_arn = aws_lb_listener. graphdb_alb_http [0 ]. arn
175+ priority = 10
176+
177+ transform {
178+ type = " url-rewrite"
179+
180+ url_rewrite_config {
181+ rewrite {
182+ regex = " ^${ var . lb_context_path } (/(.*))?$"
183+ replace = " /$2"
184+ }
185+ }
186+ }
187+
188+ action {
189+ type = " redirect"
190+ redirect {
191+ protocol = " HTTP"
192+ port = " 80"
193+ status_code = " HTTP_301"
194+ host = " #{host}"
195+ path = " /${ trim (var. lb_context_path , " /" )} /"
196+ query = " #{query}"
197+ }
198+ }
199+
200+ condition {
201+ path_pattern {
202+ values = [" /" ]
203+ }
204+ }
205+ }
206+
207+ resource "aws_lb_listener_rule" "graphdb_root_redirect_https" {
208+ count = local. is_alb && var. lb_tls_enabled && var. lb_context_path != " " ? 1 : 0
209+
210+ listener_arn = aws_lb_listener. graphdb_alb_https [0 ]. arn
211+ priority = 10
212+
213+ transform {
214+ type = " url-rewrite"
215+
216+ url_rewrite_config {
217+ rewrite {
218+ regex = " ^${ var . lb_context_path } (/(.*))?$"
219+ replace = " /$2"
220+ }
221+ }
222+ }
223+
224+ action {
225+ type = " redirect"
226+ redirect {
227+ protocol = " HTTPS"
228+ port = " 443"
229+ status_code = " HTTP_301"
230+ host = " #{host}"
231+ path = " /${ trim (var. lb_context_path , " /" )} /"
232+ query = " #{query}"
233+ }
234+ }
235+
236+ condition {
237+ path_pattern {
238+ values = [" /" ]
239+ }
240+ }
241+ }
242+
164243resource "aws_lb_listener" "graphdb_alb_https" {
165244 count = local. is_alb && var. lb_tls_enabled ? 1 : 0
166245
@@ -170,7 +249,8 @@ resource "aws_lb_listener" "graphdb_alb_https" {
170249 certificate_arn = var. lb_tls_certificate_arn
171250 ssl_policy = var. lb_tls_policy
172251
173-
252+ # If context path is set -> default 404 (rules will handle),
253+ # else -> default forward
174254 default_action {
175255 type = var. lb_context_path != " " ? " fixed-response" : " forward"
176256
@@ -183,20 +263,42 @@ resource "aws_lb_listener" "graphdb_alb_https" {
183263 }
184264 }
185265
186- target_group_arn = var. lb_context_path == " " ? aws_lb_target_group. graphdb_alb_tg [0 ]. arn : null
266+ dynamic "forward" {
267+ for_each = var. lb_context_path == " " ? [1 ] : []
268+ content {
269+ target_group {
270+ arn = aws_lb_target_group. graphdb_alb_tg [0 ]. arn
271+ weight = 1
272+ }
273+ }
274+ }
187275 }
188276
189277 lifecycle {
190278 create_before_destroy = true
191279 }
192280}
193281
282+ # -------------------------
283+ # HTTPS context path rule
284+ # -------------------------
194285resource "aws_lb_listener_rule" "graphdb_path_based_https" {
195286 count = local. is_alb && var. lb_tls_enabled && var. lb_context_path != " " ? 1 : 0
196287
197288 listener_arn = aws_lb_listener. graphdb_alb_https [0 ]. arn
198289 priority = 100
199290
291+ transform {
292+ type = " url-rewrite"
293+
294+ url_rewrite_config {
295+ rewrite {
296+ regex = " ^${ var . lb_context_path } (/(.*))?$"
297+ replace = " /$2"
298+ }
299+ }
300+ }
301+
200302 action {
201303 type = " forward"
202304 target_group_arn = aws_lb_target_group. graphdb_alb_tg [0 ]. arn
0 commit comments