Given this Terraform script for creating an AWS Elastic Load Balancer:
resource "aws_elb" "elb" {
name = "${var.elb_name}"
subnets = ["${var.subnet_ids}"]
internal = "${var.elb_is_internal}"
security_groups = ["${var.elb_security_group}"]
listener {
instance_port = "${var.backend_port}"
instance_protocol = "${var.backend_protocol}"
lb_port = 80
lb_protocol = "http"
}
health_check {
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 3
target = "${var.health_check_target}"
interval = 30
}
cross_zone_load_balancing = true
}
How would it be modified to create multiple listeners?
aws_elbresource only allows inline blocks for listeners. However, you can do it with the new ALB, as theaws_albresource allows you to define listeners in separateaws_alb_listenerresources. You could combine those with thecountparameter and your variable to generate them dynamically. See Terraform tips & tricks: loops, if-statements, and gotchas for examples withcount. - Yevgeniy Brikman