GCP compute_engine网络接口Terraform错误

时间:2018-08-17 09:34:51

标签: google-compute-engine terraform terraform-provider-gcp

我的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:创建网络接口时出错:恰好必须提供网络或子网之一

请帮助我

1 个答案:

答案 0 :(得分:1)

高级感谢@ydaetskcoR。如果选择network_interface的自定义值。您不能同时提及networksubnetwork。您将仅选择下面提到的subnetwork值。

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