Skip to content

Commit daf7b5d

Browse files
committed
Adding features to ALB
1 parent 3b69dbd commit daf7b5d

File tree

6 files changed

+203
-9
lines changed

6 files changed

+203
-9
lines changed

action.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,12 @@ inputs:
299299
aws_alb_listen_protocol:
300300
description: "Comma-separated list of listener protocols for ALB (HTTP/HTTPS)"
301301
required: false
302+
aws_alb_redirect_enable:
303+
description: "Enable HTTP to HTTPS redirection on ALB"
304+
required: false
305+
aws_alb_www_to_apex_redirect:
306+
description: 'Enable www to apex domain redirection on ALB'
307+
required: false
302308
aws_alb_healthcheck_path:
303309
description: "Health check path for ALB target group"
304310
required: false
@@ -1355,6 +1361,8 @@ runs:
13551361
AWS_ALB_APP_PROTOCOL: ${{ inputs.aws_alb_app_protocol }}
13561362
AWS_ALB_LISTEN_PORT: ${{ inputs.aws_alb_listen_port }}
13571363
AWS_ALB_LISTEN_PROTOCOL: ${{ inputs.aws_alb_listen_protocol }}
1364+
AWS_ALB_REDIRECT_ENABLE: ${{ inputs.aws_alb_redirect_enable }}
1365+
AWS_ALB_WWW_TO_APEX_REDIRECT: ${{ inputs.aws_alb_www_to_apex_redirect }}
13581366
AWS_ALB_HEALTHCHECK_PATH: ${{ inputs.aws_alb_healthcheck_path }}
13591367
AWS_ALB_HEALTHCHECK_PROTOCOL: ${{ inputs.aws_alb_healthcheck_protocol }}
13601368
AWS_ALB_SSL_POLICY: ${{ inputs.aws_alb_ssl_policy }}

operations/_scripts/generate/generate_vars_terraform.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ if [[ $(alpha_only "$AWS_ALB_CREATE") == true ]]; then
140140
aws_alb_app_protocol=$(generate_var aws_alb_app_protocol $AWS_ALB_APP_PROTOCOL)
141141
aws_alb_listen_port=$(generate_var aws_alb_listen_port $AWS_ALB_LISTEN_PORT)
142142
aws_alb_listen_protocol=$(generate_var aws_alb_listen_protocol $AWS_ALB_LISTEN_PROTOCOL)
143+
aws_alb_redirect_enable=$(generate_var aws_alb_redirect_enable $AWS_ALB_REDIRECT_ENABLE)
144+
aws_alb_www_to_apex_redirect=$(generate_var aws_alb_www_to_apex_redirect $AWS_ALB_WWW_TO_APEX_REDIRECT)
143145
aws_alb_healthcheck_path=$(generate_var aws_alb_healthcheck_path $AWS_ALB_HEALTHCHECK_PATH)
144146
aws_alb_healthcheck_protocol=$(generate_var aws_alb_healthcheck_protocol $AWS_ALB_HEALTHCHECK_PROTOCOL)
145147
aws_alb_ssl_policy=$(generate_var aws_alb_ssl_policy $AWS_ALB_SSL_POLICY)
@@ -533,6 +535,8 @@ $aws_alb_app_port
533535
$aws_alb_app_protocol
534536
$aws_alb_listen_port
535537
$aws_alb_listen_protocol
538+
$aws_alb_redirect_enable
539+
$aws_alb_www_to_apex_redirect
536540
$aws_alb_healthcheck_path
537541
$aws_alb_healthcheck_protocol
538542
$aws_alb_ssl_policy

operations/deployment/terraform/aws/aws_variables.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,18 @@ variable "aws_alb_listen_protocol" {
364364
default = ""
365365
}
366366

367+
variable "aws_alb_redirect_enable" {
368+
type = bool
369+
description = "Enable HTTP to HTTPS redirection on ALB"
370+
default = false
371+
}
372+
373+
variable "aws_alb_www_to_apex_redirect" {
374+
type = bool
375+
description = "Enable www to apex domain redirection on ALB"
376+
default = false
377+
}
378+
367379
# Healthcheck
368380
variable "aws_alb_healthcheck_path" {
369381
type = string
@@ -382,6 +394,7 @@ variable "aws_alb_ssl_policy" {
382394
description = "SSL policy for HTTPS listeners"
383395
default = null
384396
}
397+
385398
# Logging
386399
variable "aws_alb_access_log_enabled" {
387400
type = bool

operations/deployment/terraform/aws/bitovi_main.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ module "aws_lb" {
160160
aws_alb_app_protocol = var.aws_alb_app_protocol
161161
aws_alb_listen_port = var.aws_alb_listen_port
162162
aws_alb_listen_protocol = var.aws_alb_listen_protocol
163+
aws_alb_redirect_enable = var.aws_alb_redirect_enable
164+
aws_alb_www_to_apex_redirect = var.aws_alb_www_to_apex_redirect
163165
aws_alb_healthcheck_path = var.aws_alb_healthcheck_path
164166
aws_alb_healthcheck_protocol = var.aws_alb_healthcheck_protocol
165167
aws_alb_ssl_policy = var.aws_alb_ssl_policy
@@ -172,6 +174,7 @@ module "aws_lb" {
172174
aws_vpc_subnet_selected = module.vpc.aws_selected_vpc_subnets #module.vpc.aws_vpc_subnet_selected
173175
aws_instance_server_id = module.ec2[0].aws_instance_server_id
174176
aws_alb_target_sg_id = module.ec2[0].aws_security_group_ec2_sg_id
177+
aws_r53_domain_name = var.aws_r53_domain_name
175178
# Certs
176179
aws_certificates_selected_arn = var.aws_r53_enable_cert && var.aws_r53_domain_name != "" ? module.aws_certificates[0].selected_arn : ""
177180
# Others

operations/deployment/terraform/modules/aws/lb/aws_lb.tf

Lines changed: 172 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ resource "aws_lb" "vm_alb" {
6161
# Target groups for ALB
6262
resource "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

operations/deployment/terraform/modules/aws/lb/aws_lb_vars.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ variable "aws_alb_app_port" {}
44
variable "aws_alb_app_protocol" {}
55
variable "aws_alb_listen_port" {}
66
variable "aws_alb_listen_protocol" {}
7+
variable "aws_alb_redirect_enable" {}
8+
variable "aws_alb_www_to_apex_redirect" {}
79
variable "aws_alb_healthcheck_path" {}
810
variable "aws_alb_healthcheck_protocol" {}
911
variable "aws_alb_ssl_policy" {}
10-
1112
# Logging
1213
variable "aws_alb_access_log_enabled" {}
1314
variable "aws_alb_access_log_bucket_name" {}
@@ -19,6 +20,6 @@ variable "aws_vpc_subnet_selected" {}
1920
variable "aws_instance_server_id" {}
2021
variable "aws_certificates_selected_arn" {}
2122
variable "aws_alb_target_sg_id" {}
22-
23+
variable "aws_r53_domain_name" {}
2324
variable "aws_resource_identifier" {}
2425
variable "aws_resource_identifier_supershort" {}

0 commit comments

Comments
 (0)