GCP Terraform:无法在云路由器上设置通过 BGP 通告的子网

GCP Terraform:无法在云路由器上设置通过 BGP 通告的子网

我正在尝试创建一个云路由器,并从中宣传两个网络范围,但是我从 terraform 中得到了一些非常模糊的错误,我无法完全确定问题所在。

resource "google_compute_router" "router1" {
  name     = "${var.alias}-prod1-ha-vpn-cloud-router-1"
  project  = var.project_name
  network  = var.network1
  region   = var.region1
  bgp {
    asn = var.asn
    advertise_mode = "CUSTOM"
    dynamic "advertised_ip_ranges" {
      for_each = var.advertised_ip_ranges_prod1
      content {
        range = advertised_ip_ranges_prod1.value["cidr"]
        description = advertised_ip_ranges_prod1.value["desc"]
      }
    }
  }
}

实际的模块代码:

module "vpn-ha-gateway" {
  source          = "-----"
  project_name    = var.project_name
  customer        = var.customer
  alias           = var.alias
  region1         = var.region1
  count           = (var.vpn_type == "ha-vpn" ? 1 : 0)   ## Build if vpn_type is ha-vpn
  network1        = module.prod1-vpc.self_link
  customer_redundancy = var.customer_redundancy
  customer_ha_vpn_peers = var.customer_ha_vpn_peers
  shared_secret   = var.shared_secret
  asn             = var.asn
  cust_asn        = var.cust_asn
  advertised_ip_ranges_prod1      = var.advertised_ip_ranges_prod1
}

我们将 advertised_ip_ranges_prod1 定义为一个映射,因为我们不知道每个模块有多少个:

variable "advertised_ip_ranges_prod1" {
  type = list(map(string))
  description = "advertised by BGP on prod1"
  default = [
    {
        cidr = "1.2.3.4/28" ## Prod1 advertised range
        desc = "secondary range advertised via BGP"
    },
    {
        cidr = "5.6.7.8/28" # Test advertised range
        desc = "Test range advertised via BGP"
    },
  ]
}

尽管我以相同的方式(使用 for_each)构建的其他模块工作正常,但这个模块却不断抛出这些模糊的错误:

Error: Reference to undeclared resource

  on .terraform\modules\vpn-ha\main.tf line 36, in resource "google_compute_router" "router1":
  36:         range = advertised_ip_ranges_prod1.value["cidr"]

A managed resource "advertised_ip_ranges_prod1" "value" has not been declared
in module.vpn-ha-gateway.


Error: Reference to undeclared resource

  on .terraform\modules\vpn-ha-gateway\main.tf line 37, in resource "google_compute_router" "router1":
  37:         description = advertised_ip_ranges_prod1.value["desc"]

A managed resource "advertised_ip_ranges_prod1" "value" has not been declared
in module.vpn-ha-gateway.```

答案1

因此,我认为问题在于如何在“块”内引用动态元素的当前迭代for_each。通常,用于引用for_each块中当前迭代器值的“变量”的名称实际上是动态标记本身的名称。因此,我认为您需要替换此内容:

resource "google_compute_router" "router1" {
  name     = "${var.alias}-prod1-ha-vpn-cloud-router-1"
  project  = var.project_name
  network  = var.network1
  region   = var.region1
  bgp {
    asn = var.asn
    advertise_mode = "CUSTOM"
    dynamic "advertised_ip_ranges" {
      for_each = var.advertised_ip_ranges_prod1
      content {
        range = advertised_ip_ranges_prod1.value["cidr"]
        description = advertised_ip_ranges_prod1.value["desc"]
      }
    }
  }
}

有了这个:

resource "google_compute_router" "router1" {
  name     = "${var.alias}-prod1-ha-vpn-cloud-router-1"
  project  = var.project_name
  network  = var.network1
  region   = var.region1
  bgp {
    asn = var.asn
    advertise_mode = "CUSTOM"
    dynamic "advertised_ip_ranges" {
      for_each = var.advertised_ip_ranges_prod1
      content {
        range = advertised_ip_ranges.value["cidr"]
        description = advertised_ip_ranges.value["desc"]
      }
    }
  }
}

例如s/advertised_ip_ranges_prod1/advertised_ip_ranges/

请参阅第一个例子关于此问题的文件如下:

resource "aws_elastic_beanstalk_environment" "tfenvtest" {
  name                = "tf-test-name"
  application         = "${aws_elastic_beanstalk_application.tftest.name}"
  solution_stack_name = "64bit Amazon Linux 2018.03 v2.11.4 running Go 1.12.6"

  dynamic "setting" {
    for_each = var.settings
    content {
      namespace = setting.value["namespace"]
      name = setting.value["name"]
      value = setting.value["value"]
    }
  }
}

setting.value请注意,尽管变量的名称是,但它们使用的是settings。您基本上遇到了完全相同、相当令人困惑的差异。

相关内容