如何使用Terraform在GCP计算引擎中提供多个实例。我已经尝试在资源块中使用'count‘参数。但是terraform没有提供多个实例,因为具有特定名称的VM在执行第一次计数时只创建一次。
provider "google" {
version = "3.5.0"
credentials = file("battleground01-5c86f5873d44.json")
project = "battleground01"
region = "us-east1"
zone = "us-east1-b"
}
variable "node_count" {
default = "3"
}
resource "google_compute_instance" "appserver" {
count = "${var.node_count}"
name = "battleground"
machine_type = "f1-micro"
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
network_interface {
network = "default"
}
}发布于 2022-07-13 10:46:09
为了使其工作,您必须对命名计算实例的方式做一点小小的更改:
provider "google" {
version = "3.5.0"
credentials = file("battleground01-5c86f5873d44.json")
project = "battleground01"
region = "us-east1"
zone = "us-east1-b"
}
variable "node_count" {
type = number
default = 3
}
resource "google_compute_instance" "appserver" {
count = var.node_count
name = "battleground-${count.index}"
machine_type = "f1-micro"
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
network_interface {
network = "default"
}
}在使用count元参数时,访问数组索引的方法是使用count.index属性1。您还将node_count变量默认值设置为字符串,即使它可能被Terraform转换为数字,请确保使用正确的变量类型。
https://stackoverflow.com/questions/72962977
复制相似问题