wate
4/12/2017 - 1:34 PM

sample.tf

provider sakuracloud {
  zone = "tk1v"
}

/*
 * ロードバランサーを利用するか否か
 */
variable use_loadbalanser {
  default = false
}

/*
 * Webサーバーの台数
 */
variable web_count {
  default = 1
}

data sakuracloud_archive "main" {
  os_type = "debian"
}

data sakuracloud_archive "vyos" {
  os_type = "vyos"
}

data sakuracloud_ssh_key "main" {
  filter = {
    name   = "Name"
    values = ["dummy"]
  }
}

/*
 * スイッチ
 */
resource sakuracloud_switch "main" {
  name = "switch"
}

/*
 * メンテナンス用(SSH/VPN/DHCP)
 */
resource sakuracloud_disk "vyos" {
  name              = "vyos"
  source_archive_id = "${data.sakuracloud_archive.vyos.id}"
  ssh_key_ids       = ["${data.sakuracloud_ssh_key.main.id}"]
  disable_pw_auth   = true
}

resource sakuracloud_server "vyos" {
  name                  = "vyos"
  disks                 = ["${sakuracloud_disk.vyos.id}"]
  additional_interfaces = ["${sakuracloud_switch.main.id}"]
  tags                  = ["@virtio-net-pci"]
}

/*
 * Webサーバー
 */
resource sakuracloud_disk "web" {
  name              = "web-${count.index + 1}"
  count             = "${var.web_count}"
  source_archive_id = "${data.sakuracloud_archive.main.id}"
  ssh_key_ids       = ["${data.sakuracloud_ssh_key.main.id}"]
  disable_pw_auth   = true
}

resource sakuracloud_server "web" {
  name                  = "web-${count.index + 1}"
  count                 = "${var.web_count}"
  disks                 = ["${element(sakuracloud_disk.web.*.id, count.index)}"]
  base_interface        = "${var.use_loadbalanser ? sakuracloud_switch.main.id : "shared"}"
  additional_interfaces = ["${var.use_loadbalanser ? "" : sakuracloud_switch.main.id}"]
  tags                  = ["@virtio-net-pci"]
}

/*
 * ロードバランサー
 */
resource sakuracloud_disk "loadbalanser" {
  name              = "loadbalanser"
  count             = "${var.use_loadbalanser ? 1 : 0}"
  source_archive_id = "${data.sakuracloud_archive.main.id}"
  ssh_key_ids       = ["${data.sakuracloud_ssh_key.main.id}"]
  disable_pw_auth   = true
}

resource sakuracloud_server "loadbalanser" {
  name                  = "loadbalanser"
  count                 = "${var.use_loadbalanser ? 1 : 0}"
  disks                 = ["${sakuracloud_disk.loadbalanser.id}"]
  additional_interfaces = ["${sakuracloud_switch.main.id}"]
  tags                  = ["@virtio-net-pci"]
}

/*
 * データベースサーバー
 */
resource sakuracloud_disk "database" {
  name              = "database"
  source_archive_id = "${data.sakuracloud_archive.main.id}"
  ssh_key_ids       = ["${data.sakuracloud_ssh_key.main.id}"]
  disable_pw_auth   = true
}

resource sakuracloud_server "database" {
  name           = "database"
  disks          = ["${sakuracloud_disk.database.id}"]
  base_interface = "${sakuracloud_switch.main.id}"
  tags           = ["@virtio-net-pci"]
}