【问题标题】:terraform google cloud nat using reserved static ipterraform google cloud nat 使用保留的静态 ip
【发布时间】:2019-07-01 23:27:54
【问题描述】:

我们保留了静态(列入白名单)IP 地址,需要通过 terraform 将其分配给 GCP 上的 CloudNAT。 IP 被保留并在服务提供商处注册,这需要数周时间才能获得批准并添加到他们的防火墙中,因此动态分配不是一种选择。

对我们来说主要问题是 google_compute_router_nat 部分需要 nat_ip_allocate_option,但在这种情况下,IP 地址已经分配,​​因此它会失败并出现错误说明。分配的唯一选项是 AUTO_ONLY 和 MANUAL_ONLY,但似乎可能需要 EXISTING 或 RESERVED,除非我遗漏了一些明显的东西。

这是失败的配置:

resource "google_compute_address" "static_ip" {
  name    = "whitelisted-static-ip"
  region  = "${var.project_region}"
}

resource "google_compute_router_nat" "cluster-nat" {
  name                               = "cluster-stg-nat"
  router                             = "${google_compute_router.router.name}"
  region                             = "${google_compute_router.router.region}"
  nat_ip_allocate_option             = "MANUAL_ONLY"
  nat_ips                            = ["${google_compute_address.static_ip.self_link}"]
  source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS"
  subnetwork {
    name                    = "${google_compute_subnetwork.service.self_link}"
    source_ip_ranges_to_nat = ["ALL_IP_RANGES"]
  }
}

导致以下错误:

Error: Error creating Address: googleapi: Error 409: The resource 'projects/staging-cluster/regions/us-central1/addresses/whitelisted-static-ip' already exists, alreadyExists

因为静态 IP 资源已经在 GCP 外部 IP 地址中保留并在服务提供商处注册。

【问题讨论】:

  • 我并不完全清楚您描述的是什么错误。您能否粘贴您正在运行的确切 terraform 和完整的错误输出?
  • 已编辑以删除配置的注释掉部分。希望这更清楚一点。

标签: terraform


【解决方案1】:

看起来问题出在google_compute_address resource,而不是 NAT。您正在尝试创建一个已经存在的资源。相反,您应该执行以下操作之一:

【讨论】:

    【解决方案2】:

    将 google_compute_address 资源更改为数据对象非常神奇。我修改为:

    data "google_compute_address" "static_ip" {
      name    = "whitelisted-static-ip"
      region  = "${var.project_region}"
    }
    

    “whitelisted-static-ip”的名称是我们在创建时分配给保留的外部 IP 地址的名称。更新后的路由器 NAT 资源变为:

    resource "google_compute_router_nat" "cluster-nat" {
      name                               = "${var.cluster_name}-nat"
      router                             = "${google_compute_router.router.name}"
      region                             = "${google_compute_router.router.region}"
      nat_ip_allocate_option             = "MANUAL_ONLY"
      nat_ips                            = ["${data.google_compute_address.static_ip.self_link}"]
      source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS"
      subnetwork {
        name                    = "${google_compute_subnetwork.service.self_link}"
        source_ip_ranges_to_nat = ["PRIMARY_IP_RANGE"]
      }
    }
    

    这只是 nat_ips 字段的一个 mod 以指向数据对象。简单的两个字改变,我们很高兴。 太棒了!

    【讨论】:

      猜你喜欢
      • 2021-09-19
      • 2021-11-14
      • 2021-03-12
      • 2017-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-02
      相关资源
      最近更新 更多