【问题标题】:Terraform: How to modify a public subnet's route table that was created by module 'vpc'?Terraform:如何修改由模块“vpc”创建的公共子网的路由表?
【发布时间】: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 的错误,因为我已经将它与新路由表相关联。

我该怎么做:

  1. 强制 terraform 将默认子网“忽略”到路由表设置
  2. 或者更新 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


【解决方案1】:

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

【讨论】:

  • 是的,理想情况下我不必从头开始创建整个 VPC 模块,因为我们的基础设施已经严重依赖于此。有没有办法禁止在“vpc”模块中创建/修改特定资源?
  • @BenL 不,没有办法,除非你 fork 整个模块并对其进行更改。
【解决方案2】:

感谢马尔辛的回答!我做了更多的研究并询问了一些人,看起来使用“vpc”模块来设置初始基础架构的最初决定是在无法修改单个资源的情况下做出的权衡。

我在下面采取的步骤是保留我需要的现有子网等而不破坏它们。更改子网将意味着我们不希望破坏相关的 EC2 实例等。

因此,我不得不执行逐个资源重新创建基础架构资源的繁琐步骤。以下是我为对未来感兴趣的人提供的步骤:

  1. 运行 terraform state list 以查看与 vpc 模块关联的所有资源,您可以过滤以查看 module.networking.module.vpc.aws_eip.nat[0]、module.networking.module.vpc.aws_internet_gateway。 this[0] 等等等等

  2. 对于module.networking.module.vpc 列表下的每个资源,再次单独创建资源。例如,如果您需要一个私有子网,则创建一个新的aws_subnet 资源。这会很痛苦,但您需要将其他资源中指向 Terraform 代码中旧资源的所有链接更改为指向这些新资源。

  3. 创建每个单独的资源后,告诉 Terraform 将现有资源指向您要创建的新资源(记得先terraform init,以便 Terraform 知道新资源),例如 terraform state mv module.networking.module.vpc.aws_subnet.private[0] module.vpc.aws_subnet.private_subnet_1

【讨论】:

    猜你喜欢
    • 2021-06-15
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 2020-06-25
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    相关资源
    最近更新 更多