-1

我在 IBM 云中有一个使用 Terraform 创建的虚拟服务器

resource "ibm_is_instance" "vsi1" {
  name    = "${local.BASENAME}-vsi1"
  vpc     = ibm_is_vpc.vpc.id
  zone    = local.ZONE
  keys    = [data.ibm_is_ssh_key.ssh_key_id.id]
  image   = data.ibm_is_image.ubuntu.id
  profile = "cc1-2x4"

  primary_network_interface {
    subnet          = ibm_is_subnet.subnet1.id
    security_groups = [ibm_is_security_group.sg1.id]
  }
}

如何使用 Terraform For 循环创建虚拟服务器

vsi1 , vsi2, vsi3, vsi4, vsi5

完整代码请参考IBM Cloud Terraform 入门教程

4

1 回答 1

2

您可能不需要fororfor-each循环来实现您所需要的。一个简单count的将完成所需的。添加计数(实例数)后,您需要做的就是传入count.indexVSI 名称。

resource "ibm_is_instance" "vsi" {
  count   = 4
  name    = "${local.BASENAME}-vsi-${count.index}"
  vpc     = ibm_is_vpc.vpc.id
  zone    = local.ZONE
  keys    = [data.ibm_is_ssh_key.ssh_key_id.id]
  image   = data.ibm_is_image.ubuntu.id
  profile = "cc1-2x4"

  primary_network_interface {
    subnet          = ibm_is_subnet.subnet1.id
    security_groups = [ibm_is_security_group.sg1.id]
  }
}

这将创建名称为 vsi-0,vsi-1 的实例...

于 2020-07-17T02:48:08.863 回答