【问题标题】:Terraform `import` for `aws_route_table` Wants to Delete Routes from State File Afterwards`aws_route_table` 的 Terraform `import` 之后想要从状态文件中删除路由
【发布时间】:2018-10-11 05:33:10
【问题描述】:

我在将现有 AWS 路由表导入 Terraform 时遇到了一些问题。它们导入,并且它们的路由记录在状态文件中,但之后运行 planapply 总是想删除这些路由,即使它们也在 Terraform 中定义。

我在 Terraform 中定义了一个现有的 AWS 路由表,如下所示:

resource "aws_route_table" "public_staging" {
  vpc_id = "${aws_vpc.staging.id}"

  route {
    cidr_block = "${aws_vpc.management.cidr_block}"
    vpc_peering_connection_id = "${aws_vpc_peering_connection.management_to_staging.id}"
  }
  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = "${aws_internet_gateway.staging.id}"
  }

  tags {
    Name = "public staging (igw)"
    environment = "staging"
  }
}

然后像这样导入它; terraform import aws_route_table.public_management rtb-abc123.

哪些输出:

aws_route_table.public_staging: Importing from ID "rtb-abc123"...
aws_route_table.public_staging: Import complete!
  Imported aws_route_table (ID: rtb-abc123)
  Imported aws_route (ID: r-rtb-abc123123456)
  Imported aws_route (ID: r-rtb-abc123654321)
  Imported aws_route_table_association (ID: rtbassoc-qwert765)
  Imported aws_main_route_table_association (ID: rtbassoc-asdf9876)
aws_route.public_staging: Refreshing state... (ID: r-rtb-abc123123456)
aws_route_table.public_staging: Refreshing state... (ID: rtb-abc123)
aws_route.public_staging-1: Refreshing state... (ID: r-rtb-abc123654321)
aws_route_table_association.public_staging: Refreshing state... (ID: rtbassoc-qwert765)
aws_main_route_table_association.public_staging: Refreshing state... (ID: rtbassoc-asdf9876)

然后在运行terraform plan 时,Terraform 想要删除它在状态文件中生成的所有aws_route 资源状态创建我们刚刚导入的路由表:

Terraform will perform the following actions:

  - aws_route.public_staging

  - aws_route.public_staging-1

  + aws_route_table.public_management
    ...

我还尝试在 aws_route_table 资源之外单独定义路由,并按 ID 将它们附加到路由表,如下所示:

resource "aws_route" "management_to_staging" {
  route_table_id = "${aws_route_table.public_management.id}"
  cidr_block = "${aws_vpc.staging.cidr_block}"
  vpc_peering_connection_id = "${aws_vpc_peering_connection.management_to_staging.id}"
}

唯一会导致无变化状态的事情是,如果我在路由表上运行导入,定义路由表之外的路由(如aws_route 资源),然后进入并手动将状​​态文件中生成的名称更改为我定义的tf文件。但是,我相信这实际上不会在新的运行中起作用,因为在 aws_route_table 中定义的路由和作为单独的 aws_route 资源的路由会发生冲突。

编辑:

据我所知,最可能的解释是,在导入时,Terraform 很乐意导入路由表中的路由,但是在 plan 上,它希望使用 aws_route 资源显式声明它们。

问题在于;您无法导入 aws_route 资源,因此您当前的基础设施状态永远无法与您的 terraform 状态匹配。

我认为事后明确声明它们的原因也不起作用是,如果状态文件从import aws_route_table ... 命令获取导入的路由与从apply 生成它们并显式aws_route定义。

现在我已经喘不过气来了。

【问题讨论】:

  • 您确定规则的顺序正确吗?
  • 嗯,我按照有意义的顺序运行它们。即在尝试导入任何依赖它的东西之前创建 vpc,等等。
  • 但是 TF 不就是想把路由 1 放到路由 0 的位置,反之亦然吗?与 TF 订购事宜

标签: amazon-web-services terraform terraform-provider-aws


【解决方案1】:

也遇到了这个问题。由于完全导入路由表看起来不可行,因此我将通过 terrform 创建新路由表,然后更改关联以指向新表。这似乎是让 Terraform 管理所有资源的最简单方法。

【讨论】:

    【解决方案2】:

    您应该尝试在单独的资源aws_route 上声明您的路线。 aws_route_table 资源内的嵌套 route 对象与单独的 aws_route 工作方式相同,但后者可以被导入。 Check the documentation.

    这样试试吧。

    resource "aws_route_table" "public_staging" {
      vpc_id = "${aws_vpc.staging.id}"
    
      tags {
        Name = "public staging (igw)"
        environment = "staging"
      }
    }
     
    resource "aws_route" "public_staging_r0" {
        route_table_id = aws_route_table.public_staging.id
        cidr_block = "${aws_vpc.management.cidr_block}"
        vpc_peering_connection_id = "${aws_vpc_peering_connection.management_to_staging.id}"
    }
    
    resource "aws_route" "public_staging_r1" {
        route_table_id = aws_route_table.public_staging.id
        cidr_block = "0.0.0.0/0"
        gateway_id = "${aws_internet_gateway.staging.id}"
    }
    

    然后,正常导入aws_route_table,每个aws_route如下

    terraform import aws_route.<route_id> <route_table_id>_<cidr_block>
    terraform import aws_route.public_staging_r0 rtb-abc123_0.0.0.0/0
    

    重要提示:根据文档,请勿将单独的 aws_route 资源一起与嵌套路由一起使用

    关于路由表和路由的注意事项: Terraform 目前提供独立的 Route 资源和 Route Table 资源,其中包含内联定义的路由。目前,您不能将带有内联路由的路由表与任何路由资源一起使用。这样做会导致规则设置冲突,并会覆盖规则。

    【讨论】:

      猜你喜欢
      • 2019-12-16
      • 2021-03-27
      • 2018-08-18
      • 1970-01-01
      • 2020-08-01
      • 1970-01-01
      • 2021-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多