【问题标题】:Terraform keeps updating aws_route_table when adding routes from VPC Peering从 VPC 对等互连添加路由时,Terraform 不断更新 aws_route_table
【发布时间】:2018-08-18 13:02:57
【问题描述】:

我创建了一个aws_vpc_peering_connection 以连接到我帐户中的 VPC。我正在使用 aws_route_table 将路由应用到每个 VPC 的路由表,使用路由表部分中的变量来设置路由。

路由表正确应用,但 terraform 想在之后每次应用它时再次应用它。其中一个 VPC 的 vpc 对等路由的 gateway_id 来自一个变量,因为数据是从另一个模块中提取的。

resource "aws_route_table" "route-table" {
  vpc_id = "${aws_vpc.us-west-2-3.id}"

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = "${aws_internet_gateway.internet-gateway.id}"
  }

  route {
    cidr_block  = "10.12.0.0/16"
    gateway_id  = "${aws_vpc_peering_connection.usw2-1-usw2-3.id}"
  }

}

每次我planapply terraform 都想更改aws_route_table

  ~ module.us-west-2-3.aws_route_table.route-table
      route.2485290482.cidr_block:                "10.12.0.0/16" => ""
      route.2485290482.egress_only_gateway_id:    "" => ""
      route.2485290482.gateway_id:                "" => ""
      route.2485290482.instance_id:               "" => ""
      route.2485290482.ipv6_cidr_block:           "" => ""
      route.2485290482.nat_gateway_id:            "" => ""
      route.2485290482.network_interface_id:      "" => ""
      route.2485290482.vpc_peering_connection_id: "pcx-0f3853c43363d28bb" => ""
      route.383599590.cidr_block:                 "" => "10.12.0.0/16"
      route.383599590.egress_only_gateway_id:     "" => ""
      route.383599590.gateway_id:                 "" => "pcx-0f3853c43363d28bb"
      route.383599590.instance_id:                "" => ""
      route.383599590.ipv6_cidr_block:            "" => ""
      route.383599590.nat_gateway_id:             "" => ""
      route.383599590.network_interface_id:       "" => ""
      route.383599590.vpc_peering_connection_id:  "" => ""
      route.4190671864.cidr_block:                "0.0.0.0/0" => "0.0.0.0/0"
      route.4190671864.egress_only_gateway_id:    "" => ""
      route.4190671864.gateway_id:                "igw-84caffe3" => "igw-84caffe3"
      route.4190671864.instance_id:               "" => ""
      route.4190671864.ipv6_cidr_block:           "" => ""
      route.4190671864.nat_gateway_id:            "" => ""
      route.4190671864.network_interface_id:      "" => ""
      route.4190671864.vpc_peering_connection_id: "" => ""

这是我应该报告的错误还是我做错了什么?

【问题讨论】:

    标签: terraform


    【解决方案1】:

    在您的第二个内联路由定义中,您指定了 gateway_id

    Gateway_id 用于 互联网 访问。您想改用的是 vpc_peering_connection_id

    vpc_peering_connection_id  = "${aws_vpc_peering_connection.usw2-1-usw2-3.id}"
    

    官方的 terraform 文档提到混合 gateway_id 和 nat_gateway_id 时可能会陷入这种无限更新,当你混合 gateway_id 和 vpc_peering_connection 时我不会感到惊讶:

    关于 gateway_id 和 nat_gateway_id 的注意事项:AWS API 对这两个属性非常宽容,可以使用指定为网关 ID 属性的 NAT ID 创建 aws_route_table 资源。这将导致您的配置和状态文件之间存在永久差异,因为 API 在返回的路由表中返回正确的参数。如果您的 aws_route_table 资源经常出现差异,首先要检查的是您是否指定了 NAT ID 而不是网关 ID,反之亦然。

    来源:https://www.terraform.io/docs/providers/aws/r/route_table.html

    【讨论】:

    • 非常感谢。 Terraform 在处理我的错误时能做到这一点真是太神奇了。
    猜你喜欢
    • 2019-07-14
    • 2017-06-08
    • 2017-09-13
    • 2022-01-26
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-30
    相关资源
    最近更新 更多