展平合成图的输出内容

时间:2019-07-26 04:08:39

标签: terraform

我有两个分别输出的模块

output "discovery_service_hostname" { 
  value = "${aws_appmesh_virtual_service.service.name}"
}

output "discovery_service_arn" {
  value = zipmap( aws_service_discovery_service.sd[*].name, aws_service_discovery_service.sd[*].arn)
}

两者都在输出的主脚本中使用

output "services" { 
  value = {    
    "web"             = "${module.web.discovery_service_hostname}"
    "wwb-backend"     = "${module.web_backend.discovery_service_hostname}"
    "wwb-backend-n"     = "${module.web_backend_n.discovery_service_hostname}"
  }
}

在这种情况下,我将第一个模块用于web和web-后端,而我将第二个模块用于web-backend-n

我需要在第三个脚本中通过查找功能访问服务arn,但是我会避免重复整个代码来处理这两种情况

像这样的最终输出

discovery_service = {
  "web" = "arn:xxx1"
  "web-backend" = "arn:xxx2"
  "web-backend-n" = {
    "web-backend-n-1" = "arn:xxx3"
    "web-backend-n-2" = "arn:xxx4
    "web-backend-n-3" = "arn:xxx5"

}

有没有办法像这样输出

discovery_service = {
  "web" = "arn:xxx1"
  "web-backend" = "arn:xxx2"
  "web-backend-n-1" = "arn:xxx3"
  "web-backend-n-2" = "arn:xxx4
  "web-backend-n-3" = "arn:xxx5"
}

谢谢!

1 个答案:

答案 0 :(得分:0)

我会回答我自己的问题。解决方案是始终像这样始终输出映射(甚至从具有单个输出的模块中):

output "discovery_service_arn" {
  value = zipmap( [ aws_service_discovery_service.sd.name ], [ aws_service_discovery_service.sd.arn ])
}

output "discovery_service_arn" {
  value = zipmap( aws_service_discovery_service.sd[*].name, aws_service_discovery_service.sd[*].arn)
}

然后在最终脚本中使用merge获得单个地图,如

output "discovery_service" {
  value = merge(
    module.web.discovery_service_arn,
    module.web_backend.discovery_service_arn,
    module.web_backend_n.discovery_service_arn
  )
}