Skip to content

Commit ccdbc90

Browse files
committed
Add-ECS-www-to-apex-option
1 parent 9e413c0 commit ccdbc90

File tree

7 files changed

+43
-1
lines changed

7 files changed

+43
-1
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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,31 @@ resource "aws_alb_listener_rule" "redirect_based_on_path_for_http" {
191191
}
192192
}
193193

194+
resource "aws_lb_listener_rule" "redirect_www_to_apex" {
195+
count = var.aws_ecs_lb_www_to_apex_redirect && var.aws_r53_domain_name != "" ? 1 : 0
196+
listener_arn = var.aws_certificates_selected_arn != "" ? aws_alb_listener.https_redirect[0].arn : aws_alb_listener.http_redirect[0].arn
197+
priority = 10
198+
199+
condition {
200+
host_header {
201+
values = ["www.${var.aws_r53_domain_name}"]
202+
}
203+
}
204+
205+
action {
206+
type = "redirect"
207+
208+
redirect {
209+
port = var.aws_certificates_selected_arn != "" ? "443" : "80"
210+
protocol = var.aws_certificates_selected_arn != "" ? "HTTPS" : "HTTP"
211+
status_code = "HTTP_301"
212+
host = "${var.aws_r53_domain_name}"
213+
path = "/#{path}"
214+
query = "#{query}"
215+
}
216+
}
217+
}
218+
194219
resource "aws_security_group_rule" "incoming_alb_https" {
195220
count = length(aws_alb_listener.https_redirect)
196221
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)