amazon-web-services - 如何使用 Terraform 0.11 访问其中包含地图的列表列表?
问题描述
我有几个EKS 节点组。我正在尝试访问autoscaling_groups
name
所有节点组(例如name = eks-e214c586716a
)。在 0.12 中要容易得多,但我们仍在使用 0.11。
[
[
{
"autoscaling_groups" = [
{
"name" = "eks-e214c586716a"
},
]
"remote_access_security_group_id" = "sg-name1"
},
],
[
{
"autoscaling_groups" = [
{
"name" = "eks-c866f3f2edb5"
},
]
"remote_access_security_group_id" = "sg-name2"
},
],
]
这有效:aws_eks_node_group.node-group.resources.0.autoscaling_groups.0.name
但是,当我迭代时,我不成功。
count = "${length(var.nodegroups)}"
autoscaling_group_name = "${element(aws_eks_node_group.node-group.resources.*.autoscaling_groups.*.name, count.index)}"
解决方案
看起来您误读了那里的复杂数据结构。
您有一个节点组列表,该列表又包含另一个列表,该列表始终具有一个元素,该元素是具有键autoscaling_groups
和的对象remote_access_security_group_id
。然后,autoscaling_groups
键也是另一个列表,其中一个元素包含带有键的对象name
。
您这样做的尝试是使用:
"${element(aws_eks_node_group.node-group.resources.*.autoscaling_groups.*.name, count.index)}"
这就是说循环最外面的列表,然后尝试autoscaling_groups
从那里的对象获取键,此时它是一个包含 1 个元素的列表。然后,您还尝试遍历其中只有一个元素的最内层列表,因此如果您使用以下内容,您将超过索引:
"${element(aws_eks_node_group.node-group.resources.*.*.autoscaling_groups.*.name, count.index)}"
因为element
在尝试访问 1 元素列表的第二个元素时,允许环绕您不会在此处获得超出范围的索引,但是 Terraform 不允许您使用嵌套的 splat 表达式:
Error: Nested splat expression not allowed
on main.tf line 33, in resource "local_file" "asg_names":
33: content = "${element(local.eks_node_group.*.*.autoscaling_groups.*.name, count.index)}"
A splat expression (*) cannot be used inside another attribute-only splat
expression.
因此,要获取要遍历最外层列表的自动缩放组名称,然后获取该列表的第一个元素,获取autoscaling_groups
键,获取该列表的第一个元素,然后最终从name
键中获取值。
这是一个访问此数据结构的基本工作示例,使用本地变量作为输入,使用local_file
资源作为输出,以允许我们使用以下命令对其进行循环count
:
locals {
eks_node_group = [
[
{
"autoscaling_groups" = [
{
"name" = "eks-e214c586716a"
},
]
"remote_access_security_group_id" = "sg-name1"
},
],
[
{
"autoscaling_groups" = [
{
"name" = "eks-c866f3f2edb5"
},
]
"remote_access_security_group_id" = "sg-name2"
},
],
]
}
resource "local_file" "asg_names" {
count = "${length(local.eks_node_group)}"
filename = "${count.index}.output"
content = "${element(local.eks_node_group.*.0.autoscaling_groups.0.name, count.index)}"
}
运行该计划会显示以下预期输出:
# local_file.asg_names[0] will be created
+ resource "local_file" "asg_names" {
+ content = "eks-e214c586716a"
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "0.output"
+ id = (known after apply)
}
# local_file.asg_names[1] will be created
+ resource "local_file" "asg_names" {
+ content = "eks-c866f3f2edb5"
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "1.output"
+ id = (known after apply)
}
请注意,除非您想要上述的环绕功能,否则您element
应该真正使用更直接的索引语法list[index]
:
resource "local_file" "asg_names" {
count = "${length(local.eks_node_group)}"
filename = "${count.index}.output"
content = "${local.eks_node_group[count.index].0.autoscaling_groups.0.name}"
}
推荐阅读
- java - 当我尝试在基类方法中调用子类方法时,为什么会出现错误?
- grails - Grails JSON 渲染器未公开拦截器的模型
- php - 如何在 CodeIgniter 中限制和分页数组?
- python - 如何将多个字典放在一个列表中
- python - 为什么我不能在 python 多处理中关闭池之前使用 join()
- python - 使用 python 重新采样 olhc 数据帧时出错
- c++ - 在 C++ 中的另一个函数中使用函数的参数?
- reactjs - 如何在 react-countdown 中调用 onStart 函数?
- ubuntu - 类型错误:gulp.run 不是函数
- mysql - Mysql将所有表复制到具有不同名称的同一个数据库中,例如(tablename_log)