2

我在将现有 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定义的生成它们。

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

4

2 回答 2

0

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

于 2018-08-24T20:45:02.890 回答
0

您应该尝试在单独的资源上声明您的路线aws_route。资源中的嵌套route对象aws_route_table与单独的对象相同aws_route,但后者可以被导入。检查文档

像这样试试。

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_tablenormal 和 eachaws_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 当前提供独立的路由资源和具有内嵌定义的路由的路由表资源。目前,您不能将带有内联路由的路由表与任何路由资源一起使用。这样做会导致规则设置冲突,并会覆盖规则。

于 2021-03-24T14:14:54.290 回答