首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Terraform --我如何将包含"for_each“参数的资源引用到另一个资源?

Terraform --我如何将包含"for_each“参数的资源引用到另一个资源?
EN

Stack Overflow用户
提问于 2022-01-08 19:24:50
回答 1查看 1K关注 0票数 2

我创建了一个使用for_each参数生成VM列表的资源。我很难将这个资源引用到我的web_private_group

代码语言:javascript
复制
resource "google_compute_instance_group" "web_private_group" {
  name = "${format("%s","${var.gcp_resource_name}-${var.gcp_env}-vm-group")}"
  description = "Web servers instance group"
  zone = var.gcp_zone_1
  network = google_compute_network.vpc.self_link

  # I've added some attempts I've tried that do not work...
  instances = [
    //google_compute_instance.compute_engines.self_link
    //[for o in google_compute_instance.compute_engines : o.self_link]
    {for k, o in google_compute_instance.compute_engines : k => o.self_link}
    //google_compute_instance.web_private_2.self_link
  ]
    named_port {
    name = "http"
    port = "80"
  }
    named_port {
    name = "https"
    port = "443"
  }
}


# Create Google Cloud VMs 
resource "google_compute_instance" "compute_engines" {
  for_each = var.vm_instances
  name = "${format("%s","${var.gcp_resource_name}-${var.gcp_env}-each.value")}"
  machine_type = "e2-micro"
  zone         = var.gcp_zone_1
  tags         = ["ssh","http"]

  boot_disk {
    initialize_params {
      image = "debian-10"
    }
  }
}

variable "vm_instances" {
  description = "list of VM's"
  type        = set(string)
  default     = ["vm1", "vm2", "vm3"]
} 

如何正确地将compute_engines链接到web_private_group块中的web_private_group资源?

编辑:为了进一步澄清,我如何声明在我的compute_engines资源中存在多个实例的事实?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-08 20:48:14

您可能只需要按照以下方式使用splat表达式:

代码语言:javascript
复制
instances = values(google_compute_instance.compute_engines)[*].id

此外,还可以使用for表达式:

代码语言:javascript
复制
instances = [for res in google_compute_instance.compute_engines: res.id]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70635670

复制
相关文章

相似问题

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