Skip to content

Commit f9d065c

Browse files
authored
Add ecs www to apex option (#104)
* Add-ECS-www-to-apex-option * Fix listener rule * Fix with/without redirect
1 parent 9e413c0 commit f9d065c

File tree

7 files changed

+66
-13
lines changed

7 files changed

+66
-13
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ The following inputs can be used as `step.with` keys
462462
| `aws_ecs_lb_redirect_enable`| String | Toggle redirect from HTTP and/or HTTPS to the main port. |
463463
| `aws_ecs_lb_container_path`| String | Comma separated list of paths for subsequent deployed containers. Need `aws_ecs_lb_redirect_enable` to be true. eg. api. (For http://bitovi.com/api/). If you have multiple, set them to `api,monitor,prom,,` (This example is for 6 containers) |
464464
| `aws_ecs_lb_ssl_policy` | String | SSL Policy for HTTPS listener in ALB. Will default to ELBSecurityPolicy-TLS13-1-2-2021-06 if none provided. See [this link](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/create-https-listener.html) for other policies. |
465+
| `aws_ecs_lb_www_to_apex_redirect` | Boolean | Toggle redirect from www to apex domain. `aws_r53_domain_name` must be set. Defaults to `false`. |
465466
| `aws_ecs_autoscaling_enable`| Boolean | Toggle ecs autoscaling policy. |
466467
| `aws_ecs_autoscaling_max_nodes`| String | Max ammount of nodes to scale up to. |
467468
| `aws_ecs_autoscaling_min_nodes`| String | Min ammount of nodes to scale down to. |

action.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,9 @@ inputs:
901901
aws_ecs_lb_ssl_policy:
902902
description: 'SSL Policy for HTTPS listener in ALB. Will default to ELBSecurityPolicy-TLS13-1-2-2021-06 if none provided.'
903903
required: false
904+
aws_ecs_lb_www_to_apex_redirect:
905+
description: 'Toggle redirect from www to apex domain. aws_r53_domain_name must be set.'
906+
required: false
904907
aws_ecs_autoscaling_enable:
905908
description: 'Toggle ecs autoscaling policy'
906909
required: false
@@ -1514,6 +1517,7 @@ runs:
15141517
AWS_ECS_LB_REDIRECT_ENABLE: ${{ inputs.aws_ecs_lb_redirect_enable }}
15151518
AWS_ECS_LB_CONTAINER_PATH: ${{ inputs.aws_ecs_lb_container_path }}
15161519
AWS_ECS_LB_SSL_POLICY: ${{ inputs.aws_ecs_lb_ssl_policy }}
1520+
AWS_ECS_LB_WWW_TO_APEX_REDIRECT: ${{ inputs.aws_ecs_lb_www_to_apex_redirect }}
15171521
AWS_ECS_AUTOSCALING_ENABLE: ${{ inputs.aws_ecs_autoscaling_enable }}
15181522
AWS_ECS_AUTOSCALING_MAX_NODES: ${{ inputs.aws_ecs_autoscaling_max_nodes }}
15191523
AWS_ECS_AUTOSCALING_MIN_NODES: ${{ inputs.aws_ecs_autoscaling_min_nodes }}

operations/_scripts/generate/generate_vars_terraform.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ if [[ $(alpha_only "$AWS_ECS_ENABLE") == true ]]; then
349349
aws_ecs_lb_redirect_enable=$(generate_var aws_ecs_lb_redirect_enable $AWS_ECS_LB_REDIRECT_ENABLE)
350350
aws_ecs_lb_container_path=$(generate_var aws_ecs_lb_container_path $AWS_ECS_LB_CONTAINER_PATH)
351351
aws_ecs_lb_ssl_policy=$(generate_var aws_ecs_lb_ssl_policy $AWS_ECS_LB_SSL_POLICY)
352+
aws_ecs_lb_www_to_apex_redirect=$(generate_var aws_ecs_lb_www_to_apex_redirect $AWS_ECS_LB_WWW_TO_APEX_REDIRECT)
352353
aws_ecs_autoscaling_enable=$(generate_var aws_ecs_autoscaling_enable $AWS_ECS_AUTOSCALING_ENABLE)
353354
aws_ecs_autoscaling_max_nodes=$(generate_var aws_ecs_autoscaling_max_nodes $AWS_ECS_AUTOSCALING_MAX_NODES)
354355
aws_ecs_autoscaling_min_nodes=$(generate_var aws_ecs_autoscaling_min_nodes $AWS_ECS_AUTOSCALING_MIN_NODES)
@@ -712,6 +713,7 @@ $aws_ecs_lb_port
712713
$aws_ecs_lb_redirect_enable
713714
$aws_ecs_lb_container_path
714715
$aws_ecs_lb_ssl_policy
716+
$aws_ecs_lb_www_to_apex_redirect
715717
$aws_ecs_autoscaling_enable
716718
$aws_ecs_autoscaling_max_nodes
717719
$aws_ecs_autoscaling_min_nodes

operations/deployment/terraform/aws/aws_variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,12 @@ variable "aws_ecs_lb_ssl_policy" {
15351535
default = "ELBSecurityPolicy-TLS13-1-2-2021-06"
15361536
}
15371537

1538+
variable "aws_ecs_lb_www_to_apex_redirect" {
1539+
type = bool
1540+
description = "Toggle redirect from www to apex domain. Need aws_r53_domain_name variable defined."
1541+
default = false
1542+
}
1543+
15381544
variable "aws_ecs_autoscaling_enable" {
15391545
type = bool
15401546
description = "Toggle ecs autoscaling policy"

operations/deployment/terraform/aws/bitovi_main.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ module "aws_ecs" {
479479
source = "../modules/aws/ecs"
480480
count = var.aws_ecs_enable ? 1 : 0
481481
# ECS
482-
aws_ecs_service_name = var.aws_ecs_service_name
482+
aws_ecs_service_name = var.aws_ecs_service_name
483483
aws_ecs_cluster_name = var.aws_ecs_cluster_name
484484
aws_ecs_service_launch_type = var.aws_ecs_service_launch_type
485485
aws_ecs_task_type = var.aws_ecs_task_type
@@ -501,6 +501,7 @@ module "aws_ecs" {
501501
aws_ecs_lb_redirect_enable = var.aws_ecs_lb_redirect_enable
502502
aws_ecs_lb_container_path = var.aws_ecs_lb_container_path
503503
aws_ecs_lb_ssl_policy = var.aws_ecs_lb_ssl_policy
504+
aws_ecs_lb_www_to_apex_redirect = var.aws_ecs_lb_www_to_apex_redirect
504505
aws_ecs_autoscaling_enable = var.aws_ecs_autoscaling_enable
505506
aws_ecs_autoscaling_max_nodes = var.aws_ecs_autoscaling_max_nodes
506507
aws_ecs_autoscaling_min_nodes = var.aws_ecs_autoscaling_min_nodes
@@ -514,6 +515,7 @@ module "aws_ecs" {
514515
aws_selected_vpc_id = module.vpc.aws_selected_vpc_id
515516
aws_selected_subnets = module.vpc.aws_selected_vpc_subnets
516517
# Others
518+
aws_r53_domain_name = var.aws_r53_enable && var.aws_r53_domain_name != "" ? var.aws_r53_domain_name : ""
517519
aws_certificate_enabled = var.aws_r53_enable_cert && length(module.aws_certificates) > 0 ? true : false
518520
aws_certificates_selected_arn = var.aws_r53_enable_cert && var.aws_r53_domain_name != "" ? module.aws_certificates[0].selected_arn : ""
519521
aws_resource_identifier = var.aws_resource_identifier

operations/deployment/terraform/modules/aws/ecs/aws_ecs_networking.tf

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,23 +124,18 @@ resource "aws_alb_listener_rule" "redirect_based_on_path" {
124124
}
125125

126126
resource "aws_alb_listener" "http_redirect" {
127-
count = var.aws_ecs_lb_redirect_enable && !contains(local.aws_ecs_lb_port,80) ? 1 : 0
127+
count = var.aws_ecs_lb_redirect_enable && !contains(local.aws_ecs_lb_port,80) && var.aws_certificate_enabled ? 1 : 0
128128
load_balancer_arn = aws_alb.ecs_lb[0].id
129129
port = "80"
130130
protocol = "HTTP"
131131

132-
133132
default_action {
134-
type = var.aws_certificate_enabled ? "redirect" : "forward"
135-
target_group_arn = var.aws_certificate_enabled ? null : aws_alb_target_group.lb_targets[0].id
136-
137-
dynamic "redirect" {
138-
for_each = var.aws_certificate_enabled ? [1] : [0]
139-
content {
140-
port = 443
141-
protocol = "HTTPS"
142-
status_code = "HTTP_301"
143-
}
133+
type = "redirect"
134+
135+
redirect {
136+
port = "443"
137+
protocol = "HTTPS"
138+
status_code = "HTTP_301"
144139
}
145140
}
146141
depends_on = [
@@ -149,6 +144,22 @@ resource "aws_alb_listener" "http_redirect" {
149144
]
150145
}
151146

147+
resource "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
149+
load_balancer_arn = aws_alb.ecs_lb[0].id
150+
port = "80"
151+
protocol = "HTTP"
152+
153+
default_action {
154+
type = "forward"
155+
target_group_arn = aws_alb_target_group.lb_targets[0].id
156+
}
157+
depends_on = [
158+
aws_alb.ecs_lb,
159+
aws_alb_target_group.lb_targets
160+
]
161+
}
162+
152163
resource "aws_security_group_rule" "incoming_alb_http" {
153164
count = length(aws_alb_listener.http_redirect)
154165
type = "ingress"
@@ -191,6 +202,31 @@ resource "aws_alb_listener_rule" "redirect_based_on_path_for_http" {
191202
}
192203
}
193204

205+
resource "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
208+
priority = 10
209+
210+
condition {
211+
host_header {
212+
values = ["www.${var.aws_r53_domain_name}"]
213+
}
214+
}
215+
216+
action {
217+
type = "redirect"
218+
219+
redirect {
220+
port = var.aws_certificate_enabled ? "443" : "80"
221+
protocol = var.aws_certificate_enabled ? "HTTPS" : "HTTP"
222+
status_code = "HTTP_301"
223+
host = "${var.aws_r53_domain_name}"
224+
path = "/#{path}"
225+
query = "#{query}"
226+
}
227+
}
228+
}
229+
194230
resource "aws_security_group_rule" "incoming_alb_https" {
195231
count = length(aws_alb_listener.https_redirect)
196232
type = "ingress"

operations/deployment/terraform/modules/aws/ecs/aws_ecs_vars.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ variable "aws_ecs_lb_port" {}
2020
variable "aws_ecs_lb_redirect_enable" {}
2121
variable "aws_ecs_lb_container_path" {}
2222
variable "aws_ecs_lb_ssl_policy" {}
23+
variable "aws_ecs_lb_www_to_apex_redirect" {}
2324
variable "aws_ecs_autoscaling_enable" {}
2425
variable "aws_ecs_autoscaling_max_nodes" {}
2526
variable "aws_ecs_autoscaling_min_nodes" {}
@@ -29,6 +30,7 @@ variable "aws_ecs_cloudwatch_enable" {}
2930
variable "aws_ecs_cloudwatch_lg_name" {}
3031
variable "aws_ecs_cloudwatch_skip_destroy" {}
3132
variable "aws_ecs_cloudwatch_retention_days" {}
33+
variable "aws_r53_domain_name" {}
3234
variable "aws_certificate_enabled" {}
3335
variable "aws_certificates_selected_arn" {}
3436
variable "aws_region_current_name" {}

0 commit comments

Comments
 (0)