Terraform - 如何将包含 "for_each" 参数的资源引用到另一个资源?
Terraform - How can I reference a resource containing a "for_each" argument to another resource?
我使用 for_each
参数创建了一个生成 VM 列表的资源。我在尝试将此资源引用到我的 web_private_group
.
时遇到问题
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
link 连接到 instances= []
块中的 web_private_group
资源?
编辑:为了进一步说明,我如何说明我的 compute_engines
资源中有多个实例这一事实?
您可能只需要使用如下 splat
表达式:
instances = values(google_compute_instance.compute_engines)[*].id
此外,还可以使用for
表达式:
instances = [for res in google_compute_instance.compute_engines: res.id]
我使用 for_each
参数创建了一个生成 VM 列表的资源。我在尝试将此资源引用到我的 web_private_group
.
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
link 连接到 instances= []
块中的 web_private_group
资源?
编辑:为了进一步说明,我如何说明我的 compute_engines
资源中有多个实例这一事实?
您可能只需要使用如下 splat
表达式:
instances = values(google_compute_instance.compute_engines)[*].id
此外,还可以使用for
表达式:
instances = [for res in google_compute_instance.compute_engines: res.id]