gpt4 book ai didi

google-cloud-platform - 错误 - 缺少资源实例键 : Because google_compute_instance has "count" set, 必须在特定实例上访问其属性

转载 作者:行者123 更新时间:2023-12-05 02:46:39 27 4
gpt4 key购买 nike

我正在尝试将在模块“微服务实例”中创建的谷歌计算实例的 mat_ip 传递给另一个模块“数据库”。由于我创建了多个实例,因此我在模块“微服务实例”中收到输出变量的以下错误。

Error: Missing resource instance key

on modules/microservice-instance/ms-outputs.tf line 3, in output "nat_ip": 3: value = google_compute_instance.apps.network_interface[*].access_config[0].nat_ip

Because google_compute_instance.apps has "count" set, its attributes must be accessed on specific instances.

For example, to correlate with indices of a referring resource, use:
google_compute_instance.apps[count.index]

我看过以下内容并使用相同的方式访问属性,但它不起作用。这是代码-主.tf

provider "google" {
credentials = "${file("../../service-account.json")}"
project = var.project
region =var.region


}

# Include modules
module "microservice-instance" {
count = var.appserver_count
source = "./modules/microservice-instance"
appserver_count = var.appserver_count
}
module "database" {
count = var.no_of_db_instances
source = "./modules/database"
nat_ip = module.microservice-instance.nat_ip
no_of_db_instances = var.no_of_db_instances
}

./modules/microservice-instance/microservice-instance.tf

resource "google_compute_instance" "apps" {
count = var.appserver_count
name = "apps-${count.index + 1}"
# name = "apps-${random_id.app_name_suffix.hex}"
machine_type = "f1-micro"

boot_disk {
initialize_params {
image = "ubuntu-os-cloud/ubuntu-1804-lts"
}
}

network_interface {
network = "default"

access_config {
// Ephemeral IP
}
}
}

./modules/microservice-instance/ms-outputs.tf

output "nat_ip" {
value = google_compute_instance.apps.network_interface[*].access_config[0].nat_ip
}

./modules/database/database.tf

resource "random_id" "db_name_suffix" {
byte_length = 4
}

resource "google_sql_database_instance" "postgres" {
name = "postgres-instance-${random_id.db_name_suffix.hex}"
database_version = "POSTGRES_11"
settings {
tier = "db-f1-micro"

ip_configuration {

dynamic "authorized_networks" {
for_each = var.nat_ip
# iterator = ip

content {
# value = ni.0.access_config.0.nat_ip
value = each.key
}
}

}
}
}

最佳答案

您正在创建 var.appserver_countgoogle_compute_instance.apps 资源。所以你将拥有:

google_compute_instance.apps[0]
google_compute_instance.apps[1]
...
google_compute_instance.apps[var.appserver_count - 1]

因此,在您的输出中,而不是:

output "nat_ip" {
value = google_compute_instance.apps.network_interface[*].access_config[0].nat_ip
}

您必须使用 [*] 引用单个 apps 资源或所有资源,例如:

output "nat_ip" {
value = google_compute_instance.apps[*].network_interface[*].access_config[0].nat_ip
}

关于google-cloud-platform - 错误 - 缺少资源实例键 : Because google_compute_instance has "count" set, 必须在特定实例上访问其属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65387335/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com