2

我正在使用 terraform 创建我的 aws 路由表及其路由。

我基于此引用: https ://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table

resource "aws_route_table" "r" {
  vpc_id = aws_vpc.default.id

  route {
    cidr_block = "10.0.1.0/24"
    gateway_id = aws_internet_gateway.main.id
  }

  route {
    ipv6_cidr_block        = "::/0"
    egress_only_gateway_id = aws_egress_only_internet_gateway.foo.id
  }

  tags = {
    Name = "main"
  }
}

我怎样才能做到不重复路线部分。我可以通过一组地图来做到这一点,它会知道我需要创建 2 条路线?

例子:

route = [
{
    cidr_block = "10.0.1.0/24"
    gateway_id = aws_internet_gateway.main.id
  },
{
    ipv6_cidr_block        = "::/0"
    egress_only_gateway_id = aws_egress_only_internet_gateway.foo.id
  }
]

我尝试过这样的事情:

resource "aws_route_table" "rt" {

  vpc_id = data.aws_vpc.main.id

  dynamic route {
    count = length(var.routes)
    for_each = var.routes
    content {
      cidr_block = lookup(route.value, "cidr_block", null)
      ipv6_cidr_block = lookup(route.value, "ipv6_cidr_block", null)

      egress_only_gateway_id = lookup(route.value, "egress_only_gateway_id", null)
      gateway_id = lookup(route.value, "gateway_id", null)
      instance_id = lookup(route.value, "instance_id", null)
      nat_gateway_id = lookup(route.value, "nat_gateway_id", null)
      local_gateway_id = lookup(route.value, "local_gateway_id", null)
      network_interface_id = lookup(route.value, "network_interface_id", null)
      transit_gateway_id = lookup(route.value, "transit_gateway_id", null)
      vpc_endpoint_id = lookup(route.value, "vpc_endpoint_id", null)
      vpc_peering_connection_id = lookup(route.value, "vpc_peering_connection_id_by_data", "false") == "true" ? data.aws_vpc_peering_connection.main[count.index].id : lookup(route.value, "vpc_peering_connection_id", null)
    }
  }
}
4

2 回答 2

2

动态块仅使用for_each,不使用count。但是,您同时使用countfor_each在您的块中:

  dynamic route {
    count = length(var.routes)
    for_each = var.routes

以上不正确,count = length(var.routes)应删除。

于 2020-12-17T04:38:52.293 回答
0

我的最终答案将是这个

resource "aws_route_table" "rt" {

  vpc_id = data.aws_vpc.main.id

  dynamic route {
    for_each = var.routes
    content {
      cidr_block = lookup(route.value, "cidr_block", null)
      ipv6_cidr_block = lookup(route.value, "ipv6_cidr_block", null)

      egress_only_gateway_id = lookup(route.value, "egress_only_gateway_id", null)
      gateway_id = lookup(route.value, "gateway_id", null)
      instance_id = lookup(route.value, "instance_id", null)
      nat_gateway_id = lookup(route.value, "nat_gateway_id", null)
      local_gateway_id = lookup(route.value, "local_gateway_id", null)
      network_interface_id = lookup(route.value, "network_interface_id", null)
      transit_gateway_id = lookup(route.value, "transit_gateway_id", null)
      vpc_endpoint_id = lookup(route.value, "vpc_endpoint_id", null)
      vpc_peering_connection_id = lookup(route.value, "vpc_peering_connection_id_by_data", "false") == "true" ? data.aws_vpc_peering_connection.main[route.key].id : lookup(route.value, "vpc_peering_connection_id", null)
    }
  }
}
于 2020-12-17T06:10:18.580 回答