创建 efs-server 并为 AWS EKS 设置安装它时出错
error during creation efs-server and mounting it for the AWS EKS setup
我正在尝试创建一个 efs 服务器并挂载并在那里遇到一些错误。下面是我正在使用的代码 -
variable "subnets_nodes" {}
variable "security_group_nodes" {}
resource "aws_efs_mount_target" "efs-mt" {
count = length(var.subnets_nodes)
file_system_id = aws_efs_file_system.efs.id
subnet_id = "${var.subnets_nodes[count.index].id}"
security_groups = ["${var.security_group_nodes.id}"]
}
我收到的错误是 -
Error: Invalid index
on ../modules/ops_bootstrap/modules/fluentd/main.tf line 44, in resource "aws_efs_mount_target" "efs-mt":
44: subnet_id = var.subnets_nodes[count.index].id
|----------------
| count.index is 1
| var.subnets_nodes is object with 3 attributes
The given key does not identify an element in this collection value.
Error: Invalid index
on ../modules/ops_bootstrap/modules/fluentd/main.tf line 44, in resource "aws_efs_mount_target" "efs-mt":
44: subnet_id = var.subnets_nodes[count.index].id
|----------------
| count.index is 0
| var.subnets_nodes is object with 3 attributes
The given key does not identify an element in this collection value.
Error: Invalid index
on ../modules/ops_bootstrap/modules/fluentd/main.tf line 44, in resource "aws_efs_mount_target" "efs-mt":
44: subnet_id = var.subnets_nodes[count.index].id
|----------------
| count.index is 2
| var.subnets_nodes is object with 3 attributes
给定键未标识此集合值中的元素。
任何帮助将不胜感激 ? (已编辑)
您的变量 subnet_nodes
是 object
的格式,无法通过索引访问。对象和映射使用键值对而不是索引来引用元素。我认为变量是这样的:
subnets_nodes = {
node_1 = {
id = "some-id"
}
node_2 = {
id = "some-id"
}
node_3 = {
id = "some-id"
}
}
所以本例中的第一个 id
将被 var.subnets_nodes["node_1"].id
访问,而不是 var.subnets_nodes[0].id
。
建议在此用例中使用 for_each,因为它将遍历对象,您可以在其中使用 each.value
访问值。
这优于使用 count
,因为当使用 count
时值的顺序发生变化,资源将被强制重新创建。这不是 for_each
的问题。 (An article 对此进行解释)
所以你会想做这样的事情:
resource "aws_efs_mount_target" "efs-mt" {
for_each = var.subnets_nodes
file_system_id = aws_efs_file_system.efs.id
subnet_id = each.value.id
security_groups = ["${var.security_group_nodes.id}"]
}
我正在尝试创建一个 efs 服务器并挂载并在那里遇到一些错误。下面是我正在使用的代码 -
variable "subnets_nodes" {}
variable "security_group_nodes" {}
resource "aws_efs_mount_target" "efs-mt" {
count = length(var.subnets_nodes)
file_system_id = aws_efs_file_system.efs.id
subnet_id = "${var.subnets_nodes[count.index].id}"
security_groups = ["${var.security_group_nodes.id}"]
}
我收到的错误是 -
Error: Invalid index
on ../modules/ops_bootstrap/modules/fluentd/main.tf line 44, in resource "aws_efs_mount_target" "efs-mt":
44: subnet_id = var.subnets_nodes[count.index].id
|----------------
| count.index is 1
| var.subnets_nodes is object with 3 attributes
The given key does not identify an element in this collection value.
Error: Invalid index
on ../modules/ops_bootstrap/modules/fluentd/main.tf line 44, in resource "aws_efs_mount_target" "efs-mt":
44: subnet_id = var.subnets_nodes[count.index].id
|----------------
| count.index is 0
| var.subnets_nodes is object with 3 attributes
The given key does not identify an element in this collection value.
Error: Invalid index
on ../modules/ops_bootstrap/modules/fluentd/main.tf line 44, in resource "aws_efs_mount_target" "efs-mt":
44: subnet_id = var.subnets_nodes[count.index].id
|----------------
| count.index is 2
| var.subnets_nodes is object with 3 attributes
给定键未标识此集合值中的元素。 任何帮助将不胜感激 ? (已编辑)
您的变量 subnet_nodes
是 object
的格式,无法通过索引访问。对象和映射使用键值对而不是索引来引用元素。我认为变量是这样的:
subnets_nodes = {
node_1 = {
id = "some-id"
}
node_2 = {
id = "some-id"
}
node_3 = {
id = "some-id"
}
}
所以本例中的第一个 id
将被 var.subnets_nodes["node_1"].id
访问,而不是 var.subnets_nodes[0].id
。
建议在此用例中使用 for_each,因为它将遍历对象,您可以在其中使用 each.value
访问值。
这优于使用 count
,因为当使用 count
时值的顺序发生变化,资源将被强制重新创建。这不是 for_each
的问题。 (An article 对此进行解释)
所以你会想做这样的事情:
resource "aws_efs_mount_target" "efs-mt" {
for_each = var.subnets_nodes
file_system_id = aws_efs_file_system.efs.id
subnet_id = each.value.id
security_groups = ["${var.security_group_nodes.id}"]
}