GCP上的Terraform共享VPC-静态内部IP地址

时间:2019-03-26 17:22:04

标签: google-cloud-platform terraform terraform-provider-gcp

我正在尝试编写自动化以在GCP上的共享VPC中部署实例。我有一个主机网络项目和一个服务项目。我可以在主机项目(资源“ google_compute_address”“内部”)中创建静态内部IP地址资源,在其中指定VPC主机项目(NET_HUB_PROJ),但是在创建实例时无法使用它。我收到以下错误:

google_compute_instance.compute: Error creating instance: googleapi: 
Error 400: Invalid value for field 
'resource.networkInterfaces[0].networkIP': '10.128.0.10'. IP address 
'projects/prototype-network-hub/regions/us-central1/addresses/bh-int- 
ip' (10.128.0.10) is reserved by another project., invalid

我的计算模块:

data "google_compute_image" "image" {
  name    = "${var.IMAGE_NAME}"
  project = "${var.IMAGE_PROJECT}"
}

resource "google_compute_address" "internal" {
  name         = "${var.NAME}-int-ip"
  address_type = "INTERNAL"
  address      = "${var.PRIVATE_IP}"
  subnetwork   = "${var.NET_HUB_SUBNETWORK}"
  region       = "${var.NET_HUB_REGION}"
  project      = "${var.NET_HUB_PROJ}"
}

resource "google_compute_address" "external" {
  count        = "${var.EXT_IP_CREATE ? 1 : 0}"
  name         = "${var.NAME}-ext-ip"
  address_type = "EXTERNAL"
  region       = "${var.REGION}"
}

resource "google_compute_instance" "compute" {
   depends_on  = ["google_compute_address.external"] 
   name = "${var.NAME}"
   machine_type = "${var.MACHINE_TYPE}"
   zone = "${var.ZONE}"
   can_ip_forward = "${var.CAN_IP_FORWARD}"
   deletion_protection ="${var.DELETION_PROTECTION}"
   allow_stopping_for_update = "${var.ALLOW_STOPPING_FOR_UPDATE}"
   tags = ["allow-ssh"]
   metadata = {
    "network" = "${var.NETWORK}"
    "env" = "${var.ENV}"
    "role" = "${var.ROLE}"
    "region" = "${var.REGION}"
    "zone" = "${var.ZONE}"
   }
   labels = {
    "network" = "${var.NETWORK}"
    "env" = "${var.ENV}"
    "role" = "${var.ROLE}"
    "region" = "${var.REGION}"
    "zone" = "${var.ZONE}"
    }
   boot_disk {
      device_name = "${var.NAME}"
      auto_delete = "${var.BOOT_DISK_AUTO_DELETE}"
      initialize_params {
      size = "${var.BOOT_DISK_SIZE}"
      type = "${var.BOOT_DISK_TYPE}"
      image = "${data.google_compute_image.image.self_link}"
      }
   }

network_interface {
   network_ip = "${google_compute_address.internal.address}"
   subnetwork_project = "${var.NET_HUB_PROJ}"
   subnetwork   = "projects/prototype-network-hub/regions/us-central1/subnetworks/custom"
   access_config {
      nat_ip = "${element(concat(google_compute_address.external.*.address, list("")), 0)}"
   }
}
service_account {
   scopes = ["service-control", "service-management", "logging-write", "monitoring-write", "storage-ro", "https://www.googleapis.com/auth/trace.append" ]
   }
}

最终目标是实现以下目标: enter image description here

2 个答案:

答案 0 :(得分:1)

编辑(新答案): 根据GCP文档,如果要在其他项目中的共享VPC上保留内部IP,则静态内部IP必须属于服务项目(而不是代码中的主机网络项目)。看这里: https://cloud.google.com/vpc/docs/provisioning-shared-vpc#reserve_internal_ip

在TF代码库中不太可能找到shared-vpc,因此您必须使用data来获取{{1} }。类似于以下内容:

self_link

这将在您的服务项目下创建资源,但其地址在指定子网内。

部署实例时,应在VM实例选项卡的google_compute_address列下看到已分配实例的引用。

(后代的旧答案): 不幸的是,data "google_compute_subnetwork" "subnet" { name = "${var.NET_HUB_SUBNETWORK}" project = "${var.NET_HUB_PROJ}" region = "${var.NET_HUB_REGION}" } resource "google_compute_address" "internal" { name = "${var.NAME}-int-ip" address_type = "INTERNAL" address = "${var.PRIVATE_IP}" subnetwork = "${data.google_compute_subnetwork.subnet.self_link}" } 不包含internal_ip之类的google_compute_address。解决此问题的方法是提供指向subnetwork_projectgoogle_compute_instance字段的完整URL。类似于以下内容:

subnetwork

答案 1 :(得分:0)

在下面添加我的解决方案:-

db.collection('users').where('state','!==','true').orderBy('createat','desc')