terraform - 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 地址中保留并在服务提供商处注册。
解决方案
看起来问题出在 NAT 上google_compute_address resource
,而不是 NAT 上。您正在尝试创建一个已经存在的资源。相反,您应该执行以下操作之一:
- 如果您希望 Terraform 为您将资源导入 Terraform 管理此资源,请参阅此处https://www.terraform.io/docs/import/和此处https://www.terraform.io/docs/providers/google/ r/compute_address.html#import
- 如果您不希望 Terraform 为您管理 IP 地址,则可以使用数据对象而不是资源对象。这本质上是一个只读资源查找,因此您可以在 Terraform 中引用它,但可以在其他地方管理它。请参阅此处https://www.terraform.io/docs/configuration/data-sources.html和此处https://www.terraform.io/docs/providers/google/d/datasource_compute_address.html
推荐阅读
- sql-server - 如何在 SQL Server 中为具有多条记录的一个订阅者生成相同的 GUID
- laravel - 在 Laravel 中捕获特定路由上的所有 HTTP 动词
- javascript - 本地引导、css、js 文件不起作用
- android - 当两者都可用时选择 WiFi 与蜂窝网络
- python - 如何格式化目标以训练 Keras CNN 模型?
- c# - C# 中的“命名空间”和“使用”关键字有什么区别?
- facebook - Facebook 新的严格应用设置不适用于 fboauth
- apache - .htaccess 指向文本文件中指定的默认目录
- database - 层次数据库结构
- javascript - 如何从跨度元素中删除尾随零?