使用terraform和count(with attached_disk)部署gcp实例

时间:2017-10-07 08:08:51

标签: terraform gcp

环境:

  • terraform v0.10.7
  • google cloud platform
  • 用于创建后端,变量等的各种.tf文件

问题:

我能够创建多个vm实例以及多个额外的磁盘(boot_disk在每个实例上运行正常)但我希望能够相应地将这些额外的磁盘附加到每个vm,而无需为每个vm单独添加(如果这是有道理的!)。

我到目前为止的代码是(它可以用于构建多个计算实例以及多个额外的磁盘):注意(我已经注释掉了附带的错误atm的附件_d)

# vm1.tf

variable "node_count" {
  default = "3"
 }

resource "google_compute_disk" "test-node-1-index-disk-" {
    count   = "${var.node_count}"
    name    = "test-node-1-index-disk-${count.index}-data"
    type    = "pd-standard"
    zone    = "${var.zone}"
    size    = "5"
}
resource "google_compute_instance" "test-node-" {
    count = "${var.node_count}"
    name = "test-node-${count.index}"
    machine_type = "${var.machine_type}"
    zone = "${var.zone}"

    boot_disk {
    initialize_params {
    image = "${var.image}"
    }
   }
#    attached_disk {
#        source      = "${google_compute_disk.test-node-1-index-disk-0}"
#        device_name = "${google_compute_disk.test-node-1-index-disk-0}"
#   }


    network_interface {
      network = "default"
      access_config {
        // Ephemeral IP
      }

    }
}

如果我个人.tf附件_disk没有问题。

我想要的最终状态is to be able to build multiple vm's, multiple additional disks using count and attach/assign each added disk to each vm instance with a relationship of 1:1 but preferable within a single .tf and block...

我想,我可以尝试使用post gcloud compute命令来附加(知道预期的命名约定),但我希望它更具动态性并在创建时完成。

我接近这个错误吗? 任何帮助/指针非常感谢!

THX BRY

2 个答案:

答案 0 :(得分:1)

# vm1.tf

variable "node_count" {
  default = "3"
 }

resource "google_compute_disk" "test-node-1-index-disk-" {
    count   = "${var.node_count}"
    name    = "test-node-1-index-disk-${count.index}-data"
    type    = "pd-standard"
    zone    = "${var.zone}"
    size    = "5"
}
resource "google_compute_instance" "test-node-" {
    count = "${var.node_count}"
    name = "test-node-${count.index}"
    machine_type = "${var.machine_type}"
    zone = "${var.zone}"

    boot_disk {
    initialize_params {
    image = "${var.image}"
    }
   }
    attached_disk {
        source      = "${element(google_compute_disk.test-node-1-index-disk-.*.self_link, count.index)}"
        device_name = "${element(google_compute_disk.test-node-1-index-disk-.*.name, count.index)}"
   }


    network_interface {
      network = "default"
      access_config {
        // Ephemeral IP
      }

    }
}

答案 1 :(得分:0)

如果你想要静态IP

variable "node_count" {
 default = "3"
}

resource "google_compute_address" "static-ip-address" {
  count = "${var.node_count}"
  name = "${var.tag}-static-ip-${count.index + 1}"
}

resource "google_compute_disk" "test-node-1-index-disk-" {
  count   = "${var.node_count}"
  name    = "test-node-1-index-disk-${count.index}-data"
  type    = "pd-standard"
  zone    = "${var.zone}"
  size    = "5"
}
resource "google_compute_instance" "test-node-" {
  count = "${var.node_count}"
  name = "test-node-${count.index}"
  machine_type = "${var.machine_type}"
  zone = "${var.zone}"

boot_disk {
  initialize_params {
  image = "${var.image}"
  }
}
attached_disk {
    source      = "${element(google_compute_disk.test-node-1-index-disk-.*.self_link, count.index)}"
    device_name = "${element(google_compute_disk.test-node-1-index-disk-.*.name, count.index)}"
}


network_interface {
  network = "default"
  access_config {
    nat_ip = "${element(google_compute_address.static-ip-address.*.address, count.index)}"
  }

}