Terraform 升级到 .12 导致元素出现问题

Terraform upgrade to .12 causing issue with element

我有以下用于创建 eni 的代码:

resource "aws_network_interface" "eth0" {
  private_ips     = "10.10.0.1"
  security_groups = ["${aws_security_group.secg1.id}"]
  subnet_id       = "${element(data.aws_subnet_ids.sub01.ids,0)}"

  lifecycle {
    ignore_changes = ["subnet_id"]
  }
}

以上代码在 .12 版本中停止工作,它在 .11 中工作。 我尝试以下替换元素:

"tolist(data.aws_subnet_ids.trust-sub01.ids)[0]"

和:

"index(data.aws_subnet_ids.trust-sub01.ids)[0]"

两者都不起作用它给我一个错误"The subnet ID does not exist"

您有什么理由不只是使用 aws_subnet data source。您可以拨入使用过滤器返回的子网,然后使用其中的 id 属性:

data "aws_subnet" "default" {
  vpc_id = "vpc-0dfc13e14b4e1fa57"
  filter {
    name   = "availability-zone-id"
    values = ["use1-az4"]
  }
}

resource "aws_network_interface" "eth0" {
  private_ips = "172.31.16.1"
  subnet_id = data.aws_subnet.default.id

  lifecycle {
    ignore_changes = ["subnet_id"]
  }
}

如果您必须使用 aws_subnet_ids 为每个子网创建网络接口,您可以这样做:

data "aws_subnet_ids" "default" {
  vpc_id = "vpc-0dfc13e14b4e1fa57"
}

resource "aws_network_interface" "eth0" {
  count = length(data.aws_subnet_ids.default.ids)
  subnet_id = element(tolist(data.aws_subnet_ids.default.ids),count.index)

  lifecycle {
    ignore_changes = ["subnet_id"]
  }
}