我正在努力为 Azure 中相同资源的多个专用端点设置动态专用 dns 区域记录(ACR - Azure 容器注册表)。
所以我目前已经设置了这个简单的例子。基本上,它模拟 ACR 的创建并构建需要在 DNS 中注册的记录映射,以便可以正确使用 ACR。这里的问题是,需要以某种方式从每个资源/对象的内部字段(这里是端点变量)中提取值,然后正确转换它以匹配预期的输出。这是必需的,因为在我的情况下,它是模块的组成部分,并且需要根据创建的实际资源的值动态构建。
以下示例应该可以帮助任何想要检查此问题并有所帮助的人。要验证您是否得到正确的结果,只需键入“terraform refresh”,然后应该打印输出。
terraform {
backend local {}
}
# simulates creation of multiple Azure Container Registry private endpoints
variable endpoints {
type = map
default = {
"registry" = {
custom_dns_configs = [
{
fqdn = "something.westeurope.data.azurecr.io"
ip_addresses = ["1.1.1.1",]
},
{
fqdn = "something.azurecr.io"
ip_addresses = ["1.1.1.2",]
}
]
},
"registry2" = {
custom_dns_configs = [
{
fqdn = "something.westeurope.data.azurecr.io"
ip_addresses = ["1.1.2.1",]
},
{
fqdn = "something.azurecr.io"
ip_addresses = ["1.1.2.2",]
},
]
},
#...
# "registryN" = {...}
}
}
# Question: How to write for block here to get out of it exactly what's in expected
# result having in mind that there can be multiple of "registry" blocks?
# Below line produce only single result which doesn't match expected result.
output result {
value = {for pe in var.endpoints:
pe.custom_dns_configs[0].fqdn => pe.custom_dns_configs[0].ip_addresses
}
}
# Expected result:
# result = {
# "something.westeurope.data.azurecr.io" = [
# "1.1.1.1",
# "1.1.2.1",
# ]
# "something.azurecr.io" = [
# "1.1.1.2",
# "1.1.2.2",
# ]
# }