'associate a fixed private ip address on gcp compute machine with terraform

i have this terraform to use to assign a static private ip address to a new machine i run this terraform but i still see

Error: Error creating instance: googleapi: Error 400: Invalid value for field 'resource.networkInterfaces[0].networkIP': '192.168.128.206'. IP address 'projects/prj-xxxxx/regions/europe-xxx/addresses/demo-xxxx-nodoa-c' (192.168.128.206') is reserved by another project., invalid

this i the code used

resource "google_compute_address" "static-ip" {
  for_each     = toset(var.zones)
  name         = "${local.infra_id}-nodoa-${each.key}"
  project      = var.host_project.project_id
  address_type = "INTERNAL"
  region       = var.region
  purpose    = "GCE_ENDPOINT"
  subnetwork         = var.host_project.nodoa_subnet_name

}


resource "google_compute_instance" "nodoa" {
  for_each     = toset(var.zones)
  project      = var.service_project.project_id
  name         = "${local.infra_id}-nodoa-${each.key}"
  hostname     = "${local.infra_id}-nodoa-${each.key}.${local.subdomain}"
  machine_type = "xxx"
  zone         = "${var.region}-${each.key}"
  network_interface {
    subnetwork         = var.host_project.nodoa_subnet_name
    subnetwork_project = var.host_project.project_id
    network_ip = google_compute_address.static-ip[each.key].address
  }
  boot_disk {
    initialize_params {
      image = var.rhcos_gcp_image
      size  = var.install_config_params.disk_size
      type  = "pd-ssd"
    }
    kms_key_self_link = ecr
  }


Solution 1:[1]

@cava cavamagie I guess you need to specify 'network_ip' block inside access_config block.

access_config {
    nat_ip = google_compute_address.static-ip[each.key].address
}

So in general your resource block for "google_compute_instance" should look like below

resource "google_compute_instance" "nodoa" {
  for_each     = toset(var.zones)
  project      = var.service_project.project_id
  name         = "${local.infra_id}-nodoa-${each.key}"
  hostname     = "${local.infra_id}-nodoa-${each.key}.${local.subdomain}"
  machine_type = "xxx"
  zone         = "${var.region}-${each.key}"
  network_interface {
    subnetwork         = var.host_project.nodoa_subnet_name
    subnetwork_project = var.host_project.project_id
    access_config {
    nat_ip = google_compute_address.static-ip[each.key].address
}
  }
  boot_disk {
    initialize_params {
      image = var.rhcos_gcp_image
      size  = var.install_config_params.disk_size
      type  = "pd-ssd"
    }
    kms_key_self_link = ecr
  }

Reference : How to map static IP to terraform google compute engine instance?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1