Skip to content

Commit

Permalink
Feat: register service to multiple target groups (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhd999 authored Aug 9, 2022
1 parent c3ba251 commit 8bb3eab
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
25 changes: 25 additions & 0 deletions examples/basic/extra-target-groups.tf
Original file line number Diff line number Diff line change
@@ -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"
}
}
7 changes: 7 additions & 0 deletions examples/basic/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
9 changes: 9 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
}

0 comments on commit 8bb3eab

Please sign in to comment.