首页 > 解决方案 > terraform google cloud nat 使用保留的静态 ip

问题描述

我们保留了静态(列入白名单)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

解决方案


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


推荐阅读