当我多次运行它时,我遇到了奇怪的 Terraform 行为。场景是这样的:同一区域和 aws 帐户中的两个 VPC,我想在它们之间创建对等互连。在第一轮中,Terraform 正确地在两个 vpc 及其各自的路由之间创建对等互连,但在第二轮中,TF 删除了这些路由。这是我的地形代码
provider "aws" {
region = "..."
access_key = "..."
secret_key = "..."
}
resource "aws_vpc" "vpc1" {
cidr_block = "10.0.0.0/16"
}
resource "aws_vpc" "vpc2" {
cidr_block = "10.1.0.0/16"
}
resource "aws_subnet" "public1" {
vpc_id = aws_vpc.vpc1.id
cidr_block = "10.0.1.0/24"
}
resource "aws_subnet" "private1" {
vpc_id = aws_vpc.vpc1.id
cidr_block = "10.0.2.0/24"
}
resource "aws_subnet" "public2" {
vpc_id = aws_vpc.vpc2.id
cidr_block = "10.1.1.0/24"
}
resource "aws_subnet" "private2" {
vpc_id = aws_vpc.vpc2.id
cidr_block = "10.1.2.0/24"
}
resource "aws_internet_gateway" "igw1" {
vpc_id = aws_vpc.vpc1.id
}
resource "aws_internet_gateway" "igw2" {
vpc_id = aws_vpc.vpc2.id
}
resource "aws_route_table" "public_route_table1" {
vpc_id = aws_vpc.vpc1.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw1.id
}
}
resource "aws_route_table" "public_route_table2" {
vpc_id = aws_vpc.vpc2.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw2.id
}
}
resource "aws_route_table_association" "public1_rta" {
subnet_id = aws_subnet.public1.id
route_table_id = aws_route_table.public_route_table1.id
}
resource "aws_route_table_association" "public2_rta" {
subnet_id = aws_subnet.public2.id
route_table_id = aws_route_table.public_route_table2.id
}
resource "aws_vpc_peering_connection" "peer" {
vpc_id = aws_vpc.vpc1.id
peer_vpc_id = aws_vpc.vpc2.id
auto_accept = true
}
resource "aws_route" "peer1_to_2" {
route_table_id = aws_route_table.public_route_table1.id
destination_cidr_block = aws_vpc.vpc2.cidr_block
vpc_peering_connection_id = aws_vpc_peering_connection.peer.id
depends_on = [aws_vpc_peering_connection.peer]
lifecycle {
create_before_destroy = true
}
}
resource "aws_route" "peer2_to_1" {
route_table_id = aws_route_table.public_route_table2.id
destination_cidr_block = aws_vpc.vpc1.cidr_block
vpc_peering_connection_id = aws_vpc_peering_connection.peer.id
depends_on = [aws_vpc_peering_connection.peer]
lifecycle {
create_before_destroy = true
}
}
第一次运行 TF 创建对等互连和正确的路由,并且两个 ec2 实例正确执行 ping 操作。但如果我跑第二次 TF 我就有了
# aws_route_table.public_route_table1 will be updated in-place
~ resource "aws_route_table" "public_route_table1" {
id = "rtb-0ad43422d1bc73f82"
~ route = [
- {
- cidr_block = "0.0.0.0/0"
- gateway_id = "igw-06ef35a787e283282"
# (11 unchanged attributes hidden)
},
- {
- cidr_block = "10.1.0.0/16"
- vpc_peering_connection_id = "pcx-0afbf2e62bf9a43d2"
# (11 unchanged attributes hidden)
},
+ {
+ cidr_block = "0.0.0.0/0"
+ gateway_id = "igw-06ef35a787e283282"
},
]
tags = {}
# (5 unchanged attributes hidden)
}
# aws_route_table.public_route_table2 will be updated in-place
~ resource "aws_route_table" "public_route_table2" {
id = "rtb-0f282179701ca2405"
~ route = [
- {
- cidr_block = "0.0.0.0/0"
- gateway_id = "igw-015a5ba053949c7cf"
# (11 unchanged attributes hidden)
},
- {
- cidr_block = "10.0.0.0/16"
- vpc_peering_connection_id = "pcx-0afbf2e62bf9a43d2"
# (11 unchanged attributes hidden)
},
+ {
+ cidr_block = "0.0.0.0/0"
+ gateway_id = "igw-015a5ba053949c7cf"
},
]
tags = {}
# (5 unchanged attributes hidden)
}
Plan: 0 to add, 2 to change, 0 to destroy.
应用此更改后,第三次运行 TF
# aws_route.peer1_to_2 will be created
+ resource "aws_route" "peer1_to_2" {
+ destination_cidr_block = "10.1.0.0/16"
+ id = (known after apply)
+ instance_id = (known after apply)
+ instance_owner_id = (known after apply)
+ network_interface_id = (known after apply)
+ origin = (known after apply)
+ route_table_id = "rtb-0ad43422d1bc73f82"
+ state = (known after apply)
+ vpc_peering_connection_id = "pcx-0afbf2e62bf9a43d2"
}
# aws_route.peer2_to_1 will be created
+ resource "aws_route" "peer2_to_1" {
+ destination_cidr_block = "10.0.0.0/16"
+ id = (known after apply)
+ instance_id = (known after apply)
+ instance_owner_id = (known after apply)
+ network_interface_id = (known after apply)
+ origin = (known after apply)
+ route_table_id = "rtb-0f282179701ca2405"
+ state = (known after apply)
+ vpc_peering_connection_id = "pcx-0afbf2e62bf9a43d2"
}
Plan: 2 to add, 0 to change, 0 to destroy.
怎么了?
我预计在第一次应用 terraform 后不会更改任何资源,而是继续应用并删除资源 aws_route。我正在使用所有更新的(terraform 和 aws 提供商)。
这可能是因为您混合使用了在路由表资源和
route
资源内部创建与 aws_route
内联的路由。根据文档(检查开头的两个注释):
Terraform 目前提供独立的路线资源和具有内联定义路线的路线表资源。目前,您无法将具有内联路由的路由表与任何路由资源结合使用。这样做会导致规则设置冲突并覆盖规则。
我会尝试解决以下问题:
provider "aws" {
region = "..."
access_key = "..."
secret_key = "..."
}
resource "aws_vpc" "vpc1" {
cidr_block = "10.0.0.0/16"
}
resource "aws_vpc" "vpc2" {
cidr_block = "10.1.0.0/16"
}
resource "aws_subnet" "public1" {
vpc_id = aws_vpc.vpc1.id
cidr_block = "10.0.1.0/24"
}
resource "aws_subnet" "private1" {
vpc_id = aws_vpc.vpc1.id
cidr_block = "10.0.2.0/24"
}
resource "aws_subnet" "public2" {
vpc_id = aws_vpc.vpc2.id
cidr_block = "10.1.1.0/24"
}
resource "aws_subnet" "private2" {
vpc_id = aws_vpc.vpc2.id
cidr_block = "10.1.2.0/24"
}
resource "aws_internet_gateway" "igw1" {
vpc_id = aws_vpc.vpc1.id
}
resource "aws_internet_gateway" "igw2" {
vpc_id = aws_vpc.vpc2.id
}
resource "aws_route_table" "public_route_table1" {
vpc_id = aws_vpc.vpc1.id
}
resource "aws_route" "public_route_1" {
route_table_id = aws_route_table.public_route_table1.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw1.id
}
resource "aws_route_table" "public_route_table2" {
vpc_id = aws_vpc.vpc2.id
}
resource "aws_route" "public_route_2" {
route_table_id = aws_route_table.public_route_table2.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw2.id
}
resource "aws_route_table_association" "public1_rta" {
subnet_id = aws_subnet.public1.id
route_table_id = aws_route_table.public_route_table1.id
}
resource "aws_route_table_association" "public2_rta" {
subnet_id = aws_subnet.public2.id
route_table_id = aws_route_table.public_route_table2.id
}
resource "aws_vpc_peering_connection" "peer" {
vpc_id = aws_vpc.vpc1.id
peer_vpc_id = aws_vpc.vpc2.id
auto_accept = true
}
resource "aws_route" "peer1_to_2" {
route_table_id = aws_route_table.public_route_table1.id
destination_cidr_block = aws_vpc.vpc2.cidr_block
vpc_peering_connection_id = aws_vpc_peering_connection.peer.id
depends_on = [aws_vpc_peering_connection.peer]
lifecycle {
create_before_destroy = true
}
}
resource "aws_route" "peer2_to_1" {
route_table_id = aws_route_table.public_route_table2.id
destination_cidr_block = aws_vpc.vpc1.cidr_block
vpc_peering_connection_id = aws_vpc_peering_connection.peer.id
depends_on = [aws_vpc_peering_connection.peer]
lifecycle {
create_before_destroy = true
}
}