Skip to content

Commit 4891c5e

Browse files
author
Eugene Dementiev
committed
Allow using multiple target groups.
Get rid of the service IAM role.
1 parent 3112c3a commit 4891c5e

File tree

4 files changed

+12
-74
lines changed

4 files changed

+12
-74
lines changed

data.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ data "aws_region" "current" {}
66

77

88
data "aws_lb_target_group" "TG" {
9-
count = local.balanced ? 1 : 0
9+
count = local.balanced ? length(local.target_group_names) : 0
1010

11-
name = local.target_group_name
11+
name = local.target_group_names[count.index]
1212
}

iam.tf

Lines changed: 0 additions & 66 deletions
This file was deleted.

main.tf

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ resource "aws_ecs_service" "service" {
2929
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
3030
deployment_maximum_percent = var.deployment_maximum_percent
3131

32-
iam_role = local.balanced ? aws_iam_role.task-role-alb[0].arn : ""
33-
3432
ordered_placement_strategy {
3533
type = "spread"
3634
field = "attribute:ecs.availability-zone"
@@ -42,10 +40,10 @@ resource "aws_ecs_service" "service" {
4240
}
4341

4442
dynamic "load_balancer" {
45-
for_each = local.balanced ? [1] : []
43+
for_each = local.balanced ? data.aws_lb_target_group.TG[*].arn : []
4644

4745
content {
48-
target_group_arn = data.aws_lb_target_group.TG[0].arn
46+
target_group_arn = load_balancer.value
4947
container_name = var.service_name
5048
container_port = var.container_port
5149
}

variables.tf

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ variable "target_group_name" {
1111
default = ""
1212
}
1313

14+
variable "target_group_names" {
15+
description = "Names of the ALB target groups. Defaults to the `[cluster_name]` if not set"
16+
default = []
17+
}
18+
1419
variable "environment" {
1520
description = "Enviropnment vars to pass to the container. Note: they will be visible in the task definition, so please don't pass any secrets here."
1621
type = map
@@ -80,8 +85,9 @@ variable "desired_count" {
8085
}
8186

8287
locals {
83-
balanced = var.container_port > 0
84-
target_group_name = coalesce(var.target_group_name, var.cluster_name)
88+
balanced = var.container_port > 0
89+
90+
target_group_names = coalescelist(distinct(compact(concat(var.target_group_names, list(var.target_group_name)))), list(var.cluster_name))
8591

8692
default_log_configuration = {
8793
"logDriver" = "awslogs"

0 commit comments

Comments
 (0)