Terraform - ELB 尝试重新创建而不是更新

Terraform - ELB attempts re-create instead of updating

更新:terraform destroy 未删除 ELB,但应用命令尝试重新创建。只是 运行 命令 terraform apply 尝试重新创建 ELB,即使没有对 ELB 模块进行任何更改。

会是什么原因呢?

这是我得到的错误:

Error: Error creating ELB: DuplicateLoadBalancerName: Load Balancer named myELB already exists and it is configured with different parameters. status code: 400, request id: 123456789-67b1-4fbd-95e3-17f693cdcf75

这是 ELB 代码:

# Create ELB
resource "aws_elb" "myELB" {
   depends_on = [
      aws_instance.webservers,
    ]
  for_each = toset(var.public_subnets_names)

  name = "myELB"
  subnets = [data.aws_subnet.public_subnet_ids[each.value].id]

  listener {
    instance_port = 80
    instance_protocol = "http"
    lb_port = 80
    lb_protocol = "http"
  }

  health_check {
    healthy_threshold = 2
    unhealthy_threshold = 2
    timeout = 3
    target = "HTTP:8080/"
    interval = 30
  }

  instances = data.aws_instances.webservers.ids
  cross_zone_load_balancing = true
  idle_timeout = 400
  connection_draining = true
  connection_draining_timeout = 400

  tags = {
    Name = "myELB"
  }

}

数据源如下所示:

# Get Public Subnets ID's
data "aws_subnet" "public_subnet_ids" {
  depends_on = [
    aws_subnet.subnets,
  ]

  for_each = toset(var.public_subnets_names)

  filter {
    name = "tag:Name"
    values = [each.value]
  }
}

发生这种情况是因为您正在创建 多个 ALB,每个子网中都有一个同名 myALB。这是由于以下行:

for_each = toset(var.public_subnets_names)

不确定这是你的意图还是错误,因此不清楚你想要实现什么。

更新:

你通常只使用 subnets:

subnets = data.aws_subnet_ids.subnets.ids

您将在何处使用 aws_subnet_ids 获取您的子网 ID。