You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GCP Terraform为实例绑定共享子网内部静态IP报错排查

问题原因及解决方案

错误的核心是你把内部静态IP错误配置到了access_config的nat_ip字段中:

  • nat_ip字段专门用于指定外部(公网)静态IP地址,GCP会尝试在区域的外部IP池中查找这个地址,但你提供的10.25.64.83是内部IP,自然找不到,所以抛出"指定的外部IP未找到"的错误。
  • 内部静态IP应该直接在network_interface块中通过private_ip字段指定,不需要放在access_config里(access_config是用来配置公网NAT访问的,如果你不需要公网IP,可以直接去掉这个块;如果需要公网IP,要单独创建外部静态IP再关联)。

修正后的Terraform代码

## Retrieve shared subnet 
data "google_compute_subnetwork" "shared-snet" {
  name    = "shared-snet"
  project = "shared-project"
  region  = "europe-west3"
}

## Create internal static IP address in the shared subnet
resource "google_compute_address" "shared-subnet-ip" {
  name         = "shared-subnet-ip"
  address_type = "INTERNAL"
  subnetwork   = data.google_compute_subnetwork.shared-snet.self_link
}

## Create instance with the internal static IP
resource "google_compute_instance" "my-instance" {
  name         = "my-instance"
  machine_type = "n1-standard-1"
  zone         = "europe-west3-a"
  boot_disk {
    initialize_params {
      image = "windows-server-2019-dc-core-v20230712"  # Image Windows Server 2019
      size  = 80
    }
  }
  network_interface {
    subnetwork = data.google_compute_subnetwork.shared-snet.self_link
    # 直接用private_ip关联内部静态IP
    private_ip = google_compute_address.shared-subnet-ip.address
    
    # 如果你需要公网访问,保留下面的access_config并创建外部静态IP
    # access_config {
    #   nat_ip = google_compute_address.external-ip.address
    # }
  }
}

补充说明

  1. 如果你的实例不需要公网IP,可以完全删除access_config块;
  2. 如果需要公网IP,需要额外创建一个address_type = "EXTERNAL"的google_compute_address资源,再把它的地址赋值给nat_ip。

内容的提问来源于stack exchange,提问作者Startoto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 17:53:20