Skip to content

Commit

Permalink
Allow to add extra host in listener rule host header
Browse files Browse the repository at this point in the history
  • Loading branch information
guidograzioli committed Oct 12, 2021
1 parent 6d04218 commit 556fd1d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
3 changes: 2 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -15,7 +16,7 @@ resource "aws_alb_listener_rule" "rule" {

condition {
host_header {
values = ["${local.target_host_name}"]
values = local.host_header_host_names
}
}

Expand Down
7 changes: 7 additions & 0 deletions test/infra/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,3 +38,8 @@ variable "platform_config" {
variable "aws_account_alias" {}

variable "backend_dns" {}

variable "extra_listener_host_names" {
type = list(string)
default = []
}
57 changes: 54 additions & 3 deletions test/test_tf_backend_service_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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)
Expand Down Expand Up @@ -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
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
}

0 comments on commit 556fd1d

Please sign in to comment.