diff --git a/main.tf b/main.tf index f4ff012..8649b6a 100644 --- a/main.tf +++ b/main.tf @@ -2,6 +2,7 @@ locals { logical_dns_service_name = var.override_dns_name != "" ? var.override_dns_name : replace(var.component_name, "/-service$/", "") env_prefix = var.env == "live" ? "" : "${var.env}-" target_host_name = "${local.env_prefix}${local.logical_dns_service_name}.${var.dns_domain}" + host_header_host_names = concat([local.target_host_name], var.extra_listener_host_names) } resource "aws_alb_listener_rule" "rule" { @@ -15,7 +16,7 @@ resource "aws_alb_listener_rule" "rule" { condition { host_header { - values = ["${local.target_host_name}"] + values = local.host_header_host_names } } diff --git a/test/infra/main.tf b/test/infra/main.tf index 7be2e5b..8d422a4 100644 --- a/test/infra/main.tf +++ b/test/infra/main.tf @@ -11,6 +11,8 @@ module "backend_service_routing" { vpc_id = var.platform_config["vpc"] aws_account_alias = var.aws_account_alias backend_dns = var.backend_dns + + extra_listener_host_names = var.extra_listener_host_names } # configure provider to not try too hard talking to AWS API @@ -36,3 +38,8 @@ variable "platform_config" { variable "aws_account_alias" {} variable "backend_dns" {} + +variable "extra_listener_host_names" { + type = list(string) + default = [] +} diff --git a/test/test_tf_backend_service_routing.py b/test/test_tf_backend_service_routing.py index 0160f08..301e5be 100644 --- a/test/test_tf_backend_service_routing.py +++ b/test/test_tf_backend_service_routing.py @@ -120,6 +120,56 @@ def test_create_alb_listener_rule_live(self): } } """.strip() in output + def test_create_alb_listener_rule_extrahosts(self): + # When + output = check_output([ + 'terraform', + 'plan', + '-var', 'env=live', + '-var', 'aws_account_alias=awsaccount', + '-var', 'backend_dns=testbackend.com', + '-var', 'extra_listener_host_names=["test.com","example.com"]', + '-var-file=test/platform-config/eu-west-1.json', + '-target=module.backend_service_routing.aws_alb_listener_rule.rule', + '-no-color', + 'test/infra' + ]).decode('utf-8') + + # Then + assert """ + # module.backend_service_routing.aws_alb_listener_rule.rule will be created + + resource "aws_alb_listener_rule" "rule" { + + arn = (known after apply) + + id = (known after apply) + + listener_arn = "arn:aws:alb:eu-west-1:123456789123:alb:listener" + + priority = 10 + + tags_all = (known after apply) + + + action { + + order = (known after apply) + + target_group_arn = (known after apply) + + type = "forward" + } + + + condition { + + host_header { + + values = [ + + "cognito.domain.com", + + "example.com", + + "test.com", + ] + } + } + + condition { + + + path_pattern { + + values = [ + + "*", + ] + } + } + } """.strip() in output + def test_create_aws_alb_target_group(self): # When output = check_output([ @@ -135,11 +185,12 @@ def test_create_aws_alb_target_group(self): ]).decode('utf-8') # Then - assert """# module.backend_service_routing.aws_alb_target_group.target_group will be created + assert """ + # module.backend_service_routing.aws_alb_target_group.target_group will be created + resource "aws_alb_target_group" "target_group" { + arn = (known after apply) + arn_suffix = (known after apply) - + deregistration_delay = 10 + + deregistration_delay = "10" + id = (known after apply) + lambda_multi_value_headers_enabled = false + load_balancing_algorithm_type = (known after apply) @@ -181,4 +232,4 @@ def test_create_aws_alb_target_group(self): + enabled = (known after apply) + type = (known after apply) } - } """.strip() in output + } """.strip() in output diff --git a/variables.tf b/variables.tf index 81b043f..37fb9b3 100644 --- a/variables.tf +++ b/variables.tf @@ -113,3 +113,9 @@ variable "target_type" { description = "The possible values are instance (targets are specified by instance ID) or ip (targets are specified by IP address) or lambda (targets are specified by lambda arn)" default = "instance" } + +variable "extra_listener_host_names" { + description = "A list of hostname to be included in the host header for the ALB listener rule" + type = list(string) + default = [] +}