terraform - 使用 for_each 在 terraform 函数中使用列表/元组
问题描述
我有以下来自 terraform locals 的输出。
[
[
"rt1",
"rg1",
"10.0.0.0.1/26",
],
[
"rt1",
"rg1",
"10.0.0.32/24",
],
[
"rt2",
"rg2",
"10.0.0.0.1/26",
],
[
"rt2",
"rg2",
"10.0.0.32/24",
],
[
"rt3",
"rg3",
"10.0.0.0.1/26",
],
[
"rt3",
"rg3",
"10.0.0.32/24",
],
]
下面是函数。由于输出值为 6 个集合/元组 .. 它应该循环 6 次并替换路由表、资源组和子网 cidr 中的 rt、rg 和 ip。我可以通过 count 获得输出 .. 但我想用 for_each 做到这一点
resource "azurerm_route" "route" {
count = length(local.flattened)
name = "test"
resource_group_name = ((local.flattened[count.index])[1])
route_table_name = ((local.flattened[count.index])[0])
address_prefix = ((local.flattened[count.index])[2])
next_hop_type = "VirtualAppliance"
next_hop_in_ip_address = "10.220.54.16"
}
解决方案
我认为以下应该有效:
resource "azurerm_route" "route" {
for_each = {for i,v in local.flattened: i=>v}
name = "test"
resource_group_name = each.value[0]
route_table_name = each.value[1]
address_prefix = each.value[2]
next_hop_type = "VirtualAppliance"
next_hop_in_ip_address = "10.220.54.16"
}
推荐阅读
- python - systemd 服务无法启动 bash 脚本
- javascript - 如果我将相同的事件附加到相同的元素会发生什么?
- c# - C# 中的子进程列表
- android - 在Android上的MvxTabbbedPage中未调用MvvmCross初始IMvxViewModel ViewAppeared
- python - 如何通过DLMS协议读取仪表数据?
- powershell - 从批处理文件运行 ps1 脚本时无法识别自定义类型
- java - java - 如何通过Java流API获取以某个匹配开始并以不同匹配结束的子列表?
- ruby-on-rails - 数据表 - 更新后未定义的方法“orm”
- java - Java - 尽管邮递员调用有效,但不允许使用 RestTemplate 405 方法
- templates - 将列表分配给图表中的键