conditionally add load_balancer to aws_ecs_service in terraform

885 views Asked by At

How can I conditionally add additional load_balancer to a aws_ecs_service resource in terraform 0.11.7?

ternary tricks

I can use a ternary for some scenarios - like this where I add an additional sg only if defined

security_groups = ["${aws_security_group.host_type.id}",
    "${"" != var.additional_sg ? var.additional_sg : aws_security_group.host_type.id}",
]

This results in either host_type.id, additiona_sg or host_type.id, host_type.id. It works reasonably well for a certain class of parameter.

Additional LB problem

In this case I have two classes of services, some need to register with multiple LB and some do not. Because this is a new requirement I don't want to switch to a new module as that will force a destroy/recreate which is more costly to deploy. Ideally, I'd like TF to modify in place.

resource "aws_ecs_service" "default" {
  name = "${var.service_name}"
  cluster = "${var.ecs_cluster_arn}"
  ...
  load_balancer {
    container_port = "${var.container_port}"
    container_name = "${var.env_name}-${var.service_name}"
    target_group_arn = "${var.lb_target_group_arn}"
  }

  load_balancer {
    container_port = "${var.internal_container_port}"
    container_name = "${var.env_name}-${var.service_name}"
    target_group_arn = "${var.internal_lb_target_group_arn}"
  }

  ...
}
0

There are 0 answers