【发布时间】:2018-10-11 05:33:10
【问题描述】:
我在将现有 AWS 路由表导入 Terraform 时遇到了一些问题。它们导入,并且它们的路由记录在状态文件中,但之后运行 plan 或 apply 总是想删除这些路由,即使它们也在 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