【问题标题】:deploy gcp instance using terraform and count (with attached_disk)使用 terraform 和 count 部署 gcp 实例(使用 attach_disk)
【发布时间】:2018-06-01 15:49:45
【问题描述】:

环境:

  • terraform v0.10.7
  • 谷歌云平台
  • 用于创建后端、变量等的各种 .tf 文件

问题:

我能够创建多个 vm 实例以及多个附加磁盘(boot_disk 在每个实例上都可以正常工作)但我希望能够将这些附加磁盘相应地附加到每个 vm 而不必为每个 vm 单独添加(如果这是有道理的!)。

到目前为止,我的代码是(它适用于构建多个计算实例和多个额外的磁盘):注意(我已经注释掉了attached_disk which errors atm)

# 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,attached_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 命令来附加(知道预期的命名约定),但我希望它更加动态并在创建时完成。

我是不是搞错了? 非常感谢任何帮助/指针!

谢谢 布里

【问题讨论】:

    标签: terraform gcp


    【解决方案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
          }
    
        }
    }
    

    【讨论】:

      【解决方案2】:

      如果你想要静态 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)}"
        }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2020-03-17
        • 1970-01-01
        • 2016-09-06
        • 2021-01-23
        • 2020-06-16
        • 2022-01-18
        • 2020-01-19
        • 2021-06-27
        • 2021-12-08
        相关资源
        最近更新 更多