Giuliano Zorzi
Giuliano Zorzi

Reputation: 77

flattening output contents of a composite map

I have two module that output respectively

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

and

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

Both are used in the main script that outputs

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}"
  }
}

in this case I used the 1st module for web andweb-backend, while I used the 2nd module for web-backend-n

I need to access the service arn via lookup function in a 3rd script, but I would avoid duplicating the whole code to handle the two cases

final output like this

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"

}

Is there a way to have an output like

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"
}

thanks!

Upvotes: 0

Views: 116

Answers (1)

Giuliano Zorzi
Giuliano Zorzi

Reputation: 77

I will answer my own question. Solution is to always output a map (even from the module with single outputs) like this:

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

and

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

then in the final script use merge to get a single map like

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

Upvotes: 1

Related Questions