terraform - 转换字符串 = 列表Terraform 中的地图
问题描述
我在 terraform 中有一张地图,其中键是字符串,值是字符串列表。它看起来像:
locals {
admin_account_ids_by_team_name = {
"foobar" = ["12345", "67890"]
}
}
我需要将其转换为类似的东西:
{
"foobar-12345" = { account_id = "12345", team_name = "foobar" }
"foobar-67890" = { account_id = "67890", team_name = "foobar" }
}
玩弄terraform console
我已经能够使用以下方法获得一些接近的东西:flatten([for team, account_ids in { "foobar" = ["12345", "67890"] } : [for account_id in account_ids : map("${account_id}-${team}", { account_id = account_id, team = team})]])
然而,这给了我:
[
{
"12345-foobar" = {
"account_id" = "12345"
"team" = "foobar"
}
},
{
"67890-foobar" = {
"account_id" = "67890"
"team" = "foobar"
}
},
]
解决方案
我得到了它:
map(
flatten([
for team, account_ids in { "foobar" = ["12345", "67890"] } : [
for account_id in account_ids : [
"${account_id}-${team}",
{ account_id = account_id, team = team }
]
]
])...
)
map()
接受偶数个参数,将它们解释为key, value, key, value, ...
这样,解决方案是创建一个参数列表并一次将它们全部传递给map()
。
控制台的结果:
> map(flatten([for team, account_ids in { "foobar" = ["12345", "67890"], "zazu" = ["2468", "1357"] } : [for account_id in account_ids : [ "${account_id}-${team}", { account_id = account_id, team = team }]]])...)
{
"12345-foobar" = {
"account_id" = "12345"
"team" = "foobar"
}
"1357-zazu" = {
"account_id" = "1357"
"team" = "zazu"
}
"2468-zazu" = {
"account_id" = "2468"
"team" = "zazu"
}
"67890-foobar" = {
"account_id" = "67890"
"team" = "foobar"
}
}
推荐阅读
- c# - 如何使用按钮调用另一个窗口?
- java - 使用 Java Stream 多次处理结果
- thymeleaf - th:field 忽略占位符值并设置为 0
- javascript - “TypeError:无法读取 null 的属性‘内容’”在 Jest 中运行
- python - 控制轴的标题标签颜色
- python - 如何使用 Cython 将 python 脚本转换为 Linux 独立可执行文件/二进制文件?
- java - 如何仅在线程未执行或之前未启动时启动线程,或者只应创建一个线程实例
- solr - Solr8.0.0 并非所有记录都被索引
- prestashop-1.7 - 如何更改 prestashop 1.7.4.2 中的页脚版权?
- sql-server-2008 - 来自 MS SQL 的每日、每周和每月报告