@@ -61,7 +61,7 @@ resource "aws_lb" "vm_alb" {
6161# Target groups for ALB
6262resource "aws_lb_target_group" "vm_alb_tg" {
6363 count = local. alb_ports_ammount
64- name = " ${ var . aws_resource_identifier_supershort } - ${ count . index } "
64+ name = " ${ var . aws_resource_identifier_supershort } ${ count . index } "
6565 port = local. alb_app_port [count . index ]
6666 protocol = local. alb_app_protocol [count . index ]
6767 vpc_id = var. aws_vpc_selected_id
@@ -75,14 +75,27 @@ resource "aws_lb_target_group" "vm_alb_tg" {
7575 interval = 30
7676 }
7777
78+ lifecycle {
79+ replace_triggered_by = [aws_security_group . alb_security_group . id ]
80+ }
81+
7882 tags = {
7983 Name = " ${ var . aws_resource_identifier_supershort } -${ count . index } -${ local . alb_app_port [count . index ]} "
8084 }
8185}
8286
87+ # Always exists, acts as a safe dependency wrapper
88+ resource "null_resource" "http_redirect_dep" {
89+ triggers = {
90+ id = (
91+ length (aws_alb_listener. http_redirect ) > 0
92+ ) ? aws_alb_listener.http_redirect[0 ].id : " none"
93+ }
94+ }
95+
8396# Listeners for ALB
84- resource "aws_lb_listener " "vm_alb_listener " {
85- count = local. alb_ports_ammount
97+ resource "aws_alb_listener " "lb_listener_ssl " {
98+ count = local. alb_ssl_available ? length (local . alb_ports_ammount ) : 0
8699 load_balancer_arn = aws_lb. vm_alb . arn
87100 port = local. alb_listen_port [count . index ]
88101 protocol = local. alb_listen_protocol [count . index ]
@@ -92,8 +105,162 @@ resource "aws_lb_listener" "vm_alb_listener" {
92105 target_group_arn = aws_lb_target_group. vm_alb_tg [count . index ]. arn
93106 }
94107 # https://docs.aws.amazon.com/elasticloadbalancing/latest/application/create-https-listener.html
95- ssl_policy = var. aws_certificates_selected_arn != " " ? var. aws_alb_ssl_policy : null
96- certificate_arn = var. aws_certificates_selected_arn != " " ? var. aws_certificates_selected_arn : null
108+ ssl_policy = var. aws_alb_ssl_policy
109+ certificate_arn = var. aws_certificates_selected_arn
110+ lifecycle {
111+ replace_triggered_by = [null_resource. http_redirect_dep . id ]
112+ }
113+ depends_on = [aws_alb_listener . http_redirect ]
114+ }
115+
116+ resource "aws_alb_listener" "lb_listener" {
117+ count = local. alb_ssl_available ? 0 : length (local. alb_ports_ammount )
118+ load_balancer_arn = aws_lb. vm_alb . arn
119+ port = local. alb_listen_port [count . index ]
120+ protocol = local. alb_listen_protocol [count . index ]
121+ default_action {
122+ type = " forward"
123+ target_group_arn = aws_lb_target_group. vm_alb_tg [count . index ]. arn
124+ }
125+ lifecycle {
126+ replace_triggered_by = [null_resource. http_redirect_dep . id ]
127+ }
128+ depends_on = [aws_alb_listener . http_redirect ]
129+ }
130+
131+ resource "aws_alb_listener" "http_redirect" {
132+ count = var. aws_alb_redirect_enable && ! contains (local. alb_listen_port , 80 ) && local. alb_ssl_available ? 1 : 0
133+ load_balancer_arn = aws_lb. vm_alb . arn
134+ port = " 80"
135+ protocol = " HTTP"
136+
137+ default_action {
138+ type = " redirect"
139+
140+ redirect {
141+ port = " 443"
142+ protocol = " HTTPS"
143+ status_code = " HTTP_301"
144+ }
145+ }
146+ depends_on = [
147+ aws_lb . vm_alb ,
148+ aws_lb_target_group . vm_alb_tg
149+ ]
150+ }
151+
152+ resource "aws_alb_listener" "http_forward" {
153+ count = var. aws_alb_redirect_enable && ! contains (local. alb_listen_port , 80 ) && ! local. alb_ssl_available && ! var. aws_alb_www_to_apex_redirect ? 1 : 0
154+ load_balancer_arn = aws_lb. vm_alb . arn
155+ port = " 80"
156+ protocol = " HTTP"
157+
158+ default_action {
159+ type = " forward"
160+ target_group_arn = aws_lb_target_group. vm_alb_tg [0 ]. arn
161+ }
162+ depends_on = [
163+ aws_lb . vm_alb ,
164+ aws_lb_target_group . vm_alb_tg
165+ ]
166+ }
167+
168+ resource "aws_alb_listener" "http_www_redirect" {
169+ count = var. aws_alb_redirect_enable && ! contains (local. alb_listen_port , 80 ) && ! local. alb_ssl_available && var. aws_alb_www_to_apex_redirect ? 1 : 0
170+ load_balancer_arn = aws_lb. vm_alb . arn
171+ port = " 80"
172+ protocol = " HTTP"
173+
174+ default_action {
175+ type = " fixed-response"
176+
177+ fixed_response {
178+ content_type = " text/plain"
179+ message_body = " Not Found"
180+ status_code = " 404"
181+ }
182+ }
183+ depends_on = [
184+ aws_lb . vm_alb ,
185+ aws_lb_target_group . vm_alb_tg
186+ ]
187+ }
188+
189+ resource "aws_lb_listener_rule" "http_forward_apex" {
190+ count = var. aws_alb_www_to_apex_redirect && var. aws_r53_domain_name != " " && ! local. alb_ssl_available && length (aws_alb_listener. http_www_redirect ) > 0 ? 1 : 0
191+ listener_arn = aws_alb_listener. http_www_redirect [0 ]. arn
192+ priority = 20
193+
194+ condition {
195+ host_header {
196+ values = [var . aws_r53_domain_name ]
197+ }
198+ }
199+
200+ action {
201+ type = " forward"
202+ target_group_arn = aws_alb_target_group. lb_targets [0 ]. id
203+ }
204+ }
205+
206+ resource "aws_lb_listener_rule" "redirect_www_to_apex" {
207+ count = var. aws_alb_www_to_apex_redirect && var. aws_r53_domain_name != " " && (local. alb_ssl_available ? length (aws_alb_listener. https_redirect ) > 0 : length (aws_alb_listener. http_www_redirect ) > 0 ) ? 1 : 0
208+ listener_arn = local. alb_ssl_available ? aws_alb_listener. https_redirect [0 ]. arn : aws_alb_listener. http_www_redirect [0 ]. arn
209+ priority = 10
210+
211+ condition {
212+ host_header {
213+ values = [" www.${ var . aws_r53_domain_name } " ]
214+ }
215+ }
216+
217+ action {
218+ type = " redirect"
219+
220+ redirect {
221+ port = local. alb_ssl_available ? " 443" : " 80"
222+ protocol = local. alb_ssl_available ? " HTTPS" : " HTTP"
223+ status_code = " HTTP_301"
224+ host = var. aws_r53_domain_name
225+ path = " /#{path}"
226+ query = " #{query}"
227+ }
228+ }
229+ }
230+
231+ resource "aws_security_group_rule" "incoming_alb_http" {
232+ count = length (aws_alb_listener. http_redirect ) + length (aws_alb_listener. http_forward ) + length (aws_alb_listener. http_www_redirect )
233+ type = " ingress"
234+ from_port = 80
235+ to_port = 80
236+ protocol = " tcp"
237+ cidr_blocks = [" 0.0.0.0/0" ]
238+ security_group_id = aws_security_group. alb_security_group . id
239+ }
240+
241+ resource "aws_security_group_rule" "incoming_alb_https" {
242+ count = length (aws_alb_listener. https_redirect )
243+ type = " ingress"
244+ from_port = 443
245+ to_port = 443
246+ protocol = " tcp"
247+ cidr_blocks = [" 0.0.0.0/0" ]
248+ security_group_id = aws_security_group. alb_security_group . id
249+ }
250+ # ##
251+
252+ resource "aws_alb_listener" "https_redirect" {
253+ count = var. aws_alb_redirect_enable && ! contains (local. alb_listen_port , 443 ) && local. alb_ssl_available ? 1 : 0
254+ load_balancer_arn = aws_lb. vm_alb . arn
255+ port = " 443"
256+ protocol = " HTTPS"
257+ certificate_arn = var. aws_certificates_selected_arn
258+ ssl_policy = var. aws_certificates_selected_arn != " " ? var. aws_alb_ssl_policy : " " # https://docs.aws.amazon.com/elasticloadbalancing/latest/application/create-https-listener.html
259+
260+ default_action {
261+ target_group_arn = aws_lb_target_group. vm_alb_tg [0 ]. arn
262+ type = " forward"
263+ }
97264}
98265
99266# Attach EC2 instance(s) to target group(s)
@@ -174,8 +341,6 @@ locals {
174341 length (local. alb_listen_protocol ),
175342 length (local. alb_app_protocol )
176343 )
177-
178- # Optionally, you can pad arrays if needed, but min() is safest for count
179344}
180345
181346# Outputs
0 commit comments