首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Terraform的GCP计算引擎

使用Terraform的GCP计算引擎
EN

Stack Overflow用户
提问于 2022-07-13 08:15:03
回答 1查看 214关注 0票数 0

如何使用Terraform在GCP计算引擎中提供多个实例。我已经尝试在资源块中使用'count‘参数。但是terraform没有提供多个实例,因为具有特定名称的VM在执行第一次计数时只创建一次。

代码语言:javascript
复制
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"
}

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-13 10:46:09

为了使其工作,您必须对命名计算实例的方式做一点小小的更改:

代码语言:javascript
复制
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转换为数字,请确保使用正确的变量类型。

1

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72962977

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档