【问题标题】:how do i do a dynamic route in aws _route_table?我如何在 aws_route_table 中做动态路由?
【发布时间】:2021-03-27 18:21:42
【问题描述】:

我正在使用 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)
    }
  }
}

【问题讨论】:

  • 您尝试的方法不起作用?有什么错误吗?
  • @Marcin 我的计划结果最终修改并创建了 2 个路由表,这不应该是这种情况。因为只有 1 个路由表需要修改
  • 您可以删除count = length(var.routes) 并重试吗?

标签: amazon-web-services terraform terragrunt


【解决方案1】:

Dynamic blocks 仅使用 for_each,而不是 count。但是,您在块中同时使用了 countfor_each

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

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

【讨论】:

  • 谢谢。我也设法找出了我的其他问题。 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) 我在这里使用count.index。需要更改为route.key,它解决了我的问题
【解决方案2】:

我的最终答案是这个

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-01-10
    • 1970-01-01
    • 2018-10-11
    • 2021-01-27
    • 2020-02-09
    • 2022-10-31
    • 2021-12-25
    • 2017-10-23
    • 1970-01-01
    相关资源
    最近更新 更多