I have an AWS ALB that is supposed to frontend an ECS Fargate frontend app written in Angular and a Backend that is written in NestJS. I intend to use a Single ALB for both frontend and backend and therefore, I have setup a listener rule that should take about 7 backend condition rules which are path patterns.
The error I get when I try to deploy this app is:-
updating ECS Service InvalidParameterException: load balancers can have at most 5 items.
I know the limit is about 100 items from the AWS documentation.
I have this Terraform code that is supposed to provision the infrastructure:-
resource "aws_ecs_service" "ecs_service_def" {
name = "backend-service"
task_definition = "${aws_ecs_task_definition.ecs_task_def.family}"
desired_count = 1
force_new_deployment = true
health_check_grace_period_seconds = 300
launch_type = "FARGATE"
cluster = data.aws_ecs_cluster.this.id
depends_on = [
aws_iam_role_policy.ecs_service_role_policy,
data.aws_alb_listener.selected443
]
deployment_circuit_breaker {
enable = true
rollback = true
}
network_configuration {
security_groups = ["sg-0233333337779"]
subnets = var.db_subnet_ids
}
load_balancer {
target_group_arn = aws_alb_target_group.upload.arn
container_name = "backend"
container_port = "3000"
}
load_balancer {
target_group_arn = aws_alb_target_group.search-logs.arn
container_name = "backend"
container_port = "3000"
}
load_balancer {
target_group_arn = aws_alb_target_group.log-file-info.arn
container_name = "backend"
container_port = "3000"
}
dynamic "load_balancer" {
for_each = local.ecs_load_balancers
content {
container_name = load_balancer.value.container_name
container_port = load_balancer.value.container_port
target_group_arn = load_balancer.value.target_group_arn
}
}
}
locals {
ecs_load_balancers = [
{
container_name = "backend"
container_port = "3000"
target_group_arn = aws_alb_target_group.download.arn
},
{
container_name = "backend"
container_port = "3000"
target_group_arn = aws_alb_target_group.update-log-status.arn
},
{
container_name = "backend"
container_port = "3000"
target_group_arn = aws_alb_target_group.log-upload-info.arn
},
{
container_name = "backend"
container_port = "3000"
target_group_arn = aws_alb_target_group.recent-logs.arn
}]
}
I am using dynamic block to provision the remaining rules but no matter whatever change I make to the Terraform logic, the “apply” action always errors out with the given message.
Appreciate if anyone can share a solution to this issue.