1

我的 terraform 文件如下所示:

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 {
  }
}

上面的代码创建了实例。但是当我改变然后 network_interface 块如下所述

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

VPC 模块是:

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"]
}

当我将 network_interface 更改为自定义值时。它抛出的错误是

google_compute_instance.virtual_instance:创建网络接口时出错:必须提供网络或子网之一

请帮助我

4

1 回答 1

4

提前感谢@ydaetskcoR。如果您选择自定义值network_interface. 你不能同时提到networksubnetwork。您将只选择subnetwork下面提到的值。

network_interface {
    subnetwork = "${module.vpc.subnet_name}"
    access_config {}
}
于 2018-08-20T07:42:38.143 回答