【发布时间】:2022-01-23 12:56:44
【问题描述】:
我使用vpc 模块通过以下代码创建我的VPC:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = "${var.namespace}-vpc"
cidr = "10.0.0.0/16"
azs = data.aws_availability_zones.available.names
private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24"]
#assign_generated_ipv6_cidr_block = true
create_database_subnet_group = true
enable_nat_gateway = true
single_nat_gateway = true
enable_dns_hostnames = true
enable_dns_support = true
}
此模块自动创建两个公有子网,其中包含指向互联网网关的路由表。但是,我想修改两个公共子网之一,使其具有指向我创建的防火墙的不同路由表。
我所做的是创建一个新的路由表pub_to_firewall,然后创建一个新的aws_route_table_association 以将公共子网与新的路由表相关联。
resource "aws_route_table_association" "sn_to_fw_rt_association" {
subnet_id = module.vpc.public_subnets[0]
route_table_id = aws_route_table.pub_to_firewall.id
depends_on = [
aws_route_table.pub_to_firewall,
]
}
我已经能够按照说明将原始关联导入这个新关联,并通过terraform apply 让公共子网拥有这个包含防火墙引用的新路由表。
但是,当我再次运行 terraform apply 时,terraform 现在想要回到“默认”关联:
Objects have changed outside of Terraform
Terraform detected the following changes made outside of Terraform since the last "terraform apply":
# module.networking.module.vpc.aws_route_table_association.public[0] has been deleted
- resource "aws_route_table_association" "public" {
- id = "rtbassoc-[ ]" -> null
- route_table_id = "rtb-0cabc2388adXXXXX" -> null
- subnet_id = "subnet-0a2b011cd7aXXXXX" -> null
}
Unless you have made equivalent changes to your configuration, or ignored the relevant attributes using ignore_changes, the following plan may include actions to undo or respond to these
changes.
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
~ update in-place
Terraform will perform the following actions:
# module.networking.module.vpc.aws_route_table_association.public[0] will be created
+ resource "aws_route_table_association" "public" {
+ id = (known after apply)
+ route_table_id = "rtb-0cabc2388adXXXXX"
+ subnet_id = "subnet-0a2b011cd73XXXXX"
}
我不希望重新创建此资源,因为它显然会引发 │ Error: error creating Route Table (rtb-0cabc2388adXXXXX) Association: Resource.AlreadyAssociated: the specified association for route table rtb-0cabc2388adXXXXX conflicts with an existing association 的错误,因为我已经将它与新路由表相关联。
我该怎么做:
- 强制 terraform 将默认子网“忽略”到路由表设置
- 或者更新
vpc创建的 aws_route_table_association 资源module.networking.module.vpc.aws_route_table_association.public[0]以改为引用新的路由表?
【问题讨论】:
-
您实际上需要使用此路由表进行哪些更改?您可以通过模块的参数来做到这一点,而不是在模块创建后尝试手动进行更改。
-
当前公共子网的“vpc”模块的路由表指向互联网网关。我需要路由表指向我设置的防火墙 vpce。 registry.terraform.io/modules/terraform-aws-modules/vpc/aws/… 模块似乎不允许我们修改它,只能将标签添加到公共路由表中。
-
更新答案以完全重建基础架构
标签: amazon-web-services terraform terraform-provider-aws amazon-vpc