2

My terraform file looks like this:

resource "google_compute_instance" "virtual_instance" {
  name = "${var.instance_name}"
  machine_type = "${var.instance_type}"
  zone = "${var.zone}"
  lifecycle {
    ignore_changes = ["boot_disk.0.initialize_params.0.image"]
  }
  boot_disk {
    initialize_params {
      image = "ubuntu-os-cloud/ubuntu-1604-lts"
      size = "30"
      type = "pd-standard"
    }
  }
  network_interface {
    network = "default"
    access_config {}
  }

  attached_disk {
    source = "${google_compute_disk.managed_data_disk.name}"
    mode = "READ_WRITE"
  }
  metadata {
  }
}

This above code created the instance. But when i change then network_interface block as mentioned below

  network_interface {
    network = "${module.vpc.vpc_name}"
    subnetwork = "${module.vpc.subnet_name}"
    access_config {}
  }

The VPC module is :

resource "google_compute_network" "vpc" {
 name                    = "${var.name}-vpc"
 auto_create_subnetworks = "false"
}

resource "google_compute_subnetwork" "subnet_public" {
    name = "${var.subnet_name_public}"
    ip_cidr_range = "${var.subnet_cidr_public}"
    network = "${var.name}-vpc"
    depends_on    = ["google_compute_network.vpc"]
    region      = "${var.region}"
}

resource "google_compute_firewall" "firewall" {
  name    = "${var.name}-firewall"
  network = "${google_compute_network.vpc.name}"

  allow {
    protocol = "icmp"
  }

  allow {
    protocol = "tcp"
    ports    = ["22"]
  }

  source_ranges = ["0.0.0.0/0"]
}

when I changed into network_interface to custom values. It's throwing the error is

google_compute_instance.virtual_instance: Error creating network interfaces: exactly one of network or subnetwork must be provided

Please help me on this

  • 2
    The error explains it. You can only use either `network` or `subnetwork` not both. – ydaetskcoR Aug 17 '18 at 10:57
  • Yes.When I used the `network`. it's giving the error mention **google_compute_instance.virtual_instance: Error creating instance: googleapi: Error 400: Invalid value for field 'resource.networkInterfaces[0]': ''. Subnetwork should be specified for custom subnet mode network, invalid**. But when i select custom `subnetwork` only it's successful. Thanks ydaetskcoR – Sathish Narayanan Aug 20 '18 at 07:36

1 Answers1

4

Advance Thanks to @ydaetskcoR. If you choose custom values of network_interface. You can't mention both network and subnetwork. You will choose only subnetwork values mentioned below.

network_interface {
    subnetwork = "${module.vpc.subnet_name}"
    access_config {}
}
  • Had a similar problem and tried that solution but it didn't solve it, turns out I had the subnet name wrong. – Didi Kohen Feb 21 '21 at 14:10