Terraform:如何修改由模块“vpc”创建的公共子网的路由表?

Ben*_*n L 6 amazon-web-services amazon-vpc terraform terraform-provider-aws

我用的是vpc模块通过以下代码创建我的 VPC:

\n
module "vpc" {\n  source = "terraform-aws-modules/vpc/aws"\n  name = "${var.namespace}-vpc"\n  cidr = "10.0.0.0/16"\n  azs = data.aws_availability_zones.available.names\n  private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]\n  public_subnets = ["10.0.101.0/24", "10.0.102.0/24"]\n  #assign_generated_ipv6_cidr_block = true\n  create_database_subnet_group     = true\n  enable_nat_gateway               = true\n  single_nat_gateway               = true\n  enable_dns_hostnames = true\n  enable_dns_support   = true\n}\n
Run Code Online (Sandbox Code Playgroud)\n

该模块自动创建两个公共子网,其中有一个指向互联网网关的路由表。但是,我想修改两个公共子网之一,以拥有指向我创建的防火墙的不同路由表。

\n

我所做的是创建一个新的路由表pub_to_firewall,然后创建一个新的aws_route_table_association路由表以将公共子网与新的路由表关联起来。

\n
resource "aws_route_table_association" "sn_to_fw_rt_association" {\n  subnet_id      = module.vpc.public_subnets[0]\n  route_table_id = aws_route_table.pub_to_firewall.id\n  depends_on = [\n    aws_route_table.pub_to_firewall,\n  ]\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我已经能够按照说明将原始关联导入到这个新关联,并terraform apply让公共子网拥有包含防火墙引用的新路由表。

\n

然而,当我terraform apply再次运行时,terraform 现在想要返回到“默认”关联:

\n
Objects have changed outside of Terraform\n\nTerraform detected the following changes made outside of Terraform since the last "terraform apply":\n\n  # module.networking.module.vpc.aws_route_table_association.public[0] has been deleted\n  - resource "aws_route_table_association" "public" {\n      - id             = "rtbassoc-[ ]" -> null\n      - route_table_id = "rtb-0cabc2388adXXXXX" -> null\n      - subnet_id      = "subnet-0a2b011cd7aXXXXX" -> null\n    }\n\n\nUnless 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\nchanges.\n\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n\nTerraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:\n  + create\n  ~ update in-place\n\nTerraform will perform the following actions:\n\n\n  # module.networking.module.vpc.aws_route_table_association.public[0] will be created\n  + resource "aws_route_table_association" "public" {\n      + id             = (known after apply)\n      + route_table_id = "rtb-0cabc2388adXXXXX"\n      + subnet_id      = "subnet-0a2b011cd73XXXXX"\n    }\n
Run Code Online (Sandbox Code Playgroud)\n

我不希望重新创建此资源,因为它会抛出一个错误,\xe2\x94\x82 Error: error creating Route Table (rtb-0cabc2388adXXXXX) Association: Resource.AlreadyAssociated: the specified association for route table rtb-0cabc2388adXXXXX conflicts with an existing association显然因为我已经将它与新的路由表关联起来。

\n

我怎样才能:

\n
    \n
  1. 强制 terraform“忽略”路由表设置的默认子网
  2. \n
  3. 或者更新vpc创建的 aws_route_table_association 资源module.networking.module.vpc.aws_route_table_association.public[0]以引用新的路由表?
  4. \n
\n

Mar*_*cin 4

您无法更改这一点,因为这就是 aws vpc 模块的工作原理。为此,您需要定制设计的 VPC。因此,您必须分叉整个模块并进行所需的更改,或者根据您的需求从头开始创建新的 VPC 模块。