2016-12-03 188 views
2

我會粘貼在這裏我terraform腳本AWS彈性負載均衡器terraform負載均衡多lisstners

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 
} 

你們能不能幫我用terraform變量來創建多個聽衆?

+0

你是說你想動態地控制監聽塊的數量嗎?因爲不幸的是無法做到這一點。你需要一個獨立的aws_elb_listener資源,使你能夠分別定義監聽器到ELB並使用它們來配置ELB,但是這個資源不存在(我不確定它實際上是否可行)。 – ydaetskcoR

+0

實際上在這個問題上有一些活動:https://github.com/hashicorp/terraform/issues/9807。所以儘管現在不可能做到,但希望它能夠成爲未來的版本。 –

+0

您目前無法使用ELB執行此操作,因爲'aws_elb'資源僅允許偵聽器使用內嵌塊。但是,您可以使用新的ALB來實現,因爲'aws_alb'資源允許您在單獨的'aws_alb_listener'資源中定義監聽器。您可以將這些與'count'參數和您的變量結合起來以動態生成它們。查看[Terraform提示和技巧:循環,if語句和疑難解答](https://blog.gruntwork.io/terraform-tips-tricks-loops-if-statements-and-gotchas-f739bbae55f9) '。 –

回答

1

您需要將地圖列表傳遞給偵聽器。

listener = [{ 
    instance_port = "${var.backend_port}" 
    instance_protocol = "${var.backend_protocol}" 
    lb_port = 80 
    lb_protocol = "http" 
    },{ 
    instance_port = "${var.backend2_port}" 
    instance_protocol = "${var.backend2_protocol}" 
    lb_port = 8080 
    lb_protocol = "http" 
    }] 

或者,

listener = ["${var.elb_listeners}"] 

var.elb_listeners哪裏是地圖的列表如在上面的第一個例子。