如何使用 for 循环在 IBM 云 Terraform 中创建虚拟服务器?
How to create Virtual servers in IBM cloud Terraform with for loop?
我在 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
您可能不需要 for
或 for-each
循环来实现您的需要。一个简单的 count
就可以满足要求。添加计数(实例数)后,您需要做的就是在 VSI 名称中传递 count.index
。
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...的实例
我在 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
您可能不需要 for
或 for-each
循环来实现您的需要。一个简单的 count
就可以满足要求。添加计数(实例数)后,您需要做的就是在 VSI 名称中传递 count.index
。
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...的实例