From 8bb3eab270762216165d0bea5a58f7d252c71b9d Mon Sep 17 00:00:00 2001 From: Mohamad Al moualem Date: Tue, 9 Aug 2022 10:41:20 +0200 Subject: [PATCH] Feat: register service to multiple target groups (#70) --- examples/basic/extra-target-groups.tf | 25 +++++++++++++++++++++++++ examples/basic/main.tf | 7 +++++++ main.tf | 9 +++++++++ variables.tf | 9 +++++++++ 4 files changed, 50 insertions(+) create mode 100644 examples/basic/extra-target-groups.tf diff --git a/examples/basic/extra-target-groups.tf b/examples/basic/extra-target-groups.tf new file mode 100644 index 0000000..d25d15d --- /dev/null +++ b/examples/basic/extra-target-groups.tf @@ -0,0 +1,25 @@ +// In case of extra target groups (extra_target_groups) + +resource "aws_lb_target_group" "extra" { + name = "${var.name_prefix}-3000" + port = 3000 // extra port the Fargate service expose + protocol = "HTTP" + target_type = "ip" + vpc_id = data.aws_vpc.main.id + tags = { + environment = "dev" + terraform = "True" + } +} + + +resource "aws_lb_listener" "extra_listener" { + load_balancer_arn = module.fargate_alb.arn + port = 9000 // The port alb listen to and, bind to the service port (3000) + protocol = "HTTP" + + default_action { + target_group_arn = aws_lb_target_group.extra.arn + type = "forward" + } +} diff --git a/examples/basic/main.tf b/examples/basic/main.tf index 537126c..16cb92d 100644 --- a/examples/basic/main.tf +++ b/examples/basic/main.tf @@ -101,6 +101,13 @@ module "fargate" { } ] + extra_target_groups = [ + { + port = 3000, + arn = aws_lb_target_group.extra.arn + } + ] + task_container_environment = { TEST_VARIABLE = "TEST_VALUE" } diff --git a/main.tf b/main.tf index 049850c..3af1315 100644 --- a/main.tf +++ b/main.tf @@ -229,6 +229,15 @@ resource "aws_ecs_service" "service" { } } + dynamic "load_balancer" { + for_each = length(var.extra_target_groups) == 0 ? [] : var.extra_target_groups + content { + container_name = var.container_name != "" ? var.container_name : var.name_prefix + container_port = load_balancer.value.port + target_group_arn = load_balancer.value.arn + } + } + deployment_controller { # The deployment controller type to use. Valid values: CODE_DEPLOY, ECS. type = var.deployment_controller_type diff --git a/variables.tf b/variables.tf index 3abefb5..affb29b 100644 --- a/variables.tf +++ b/variables.tf @@ -277,3 +277,12 @@ variable "volumes" { type = list(any) default = [] } + +variable "extra_target_groups" { + description = "List of extra target group configurations used to register a service to multiple target groups" + type = list(object({ + port = number + arn = string + })) + default = [] +}