title: Change a list of maps into a formatted string in terraform
body: <p>I'm trying to set up a Google Cloud Load Balancer and one step requires updating the named ports on the managed instance groups for which I need a formatted string to generate the command-line call. I feel like this should be something that I can automate with terraform, but I'm struggling with mapping data formats.</p>
  
  <p>I have two sets of source data.</p>
  
  <p>From the instance group resource:</p>
  
  <pre><code>data "google_compute_instance_group" "all" {
    count = "${length(var.backend)}"
    self_link = "${element(var.backend, count.index)}"
  }
  </code></pre>
  
  <p>I get the existing named ports from <code>data.google_compute_instance_group.all.*.named_port</code> in the format:</p>
  
  <pre><code>[
      [
          map[name:https port:30443],
          map[name:http port:30080]
      ],
      [
          map[name:https port:30443],
          map[port:30080 name:http]
      ]
  ]
  </code></pre>
  
  <p>I also have the ports that I want to make sure are defined in my own map:</p>
  
  <pre><code>variable "node_ports" {
    type    = "map"
    default = {
      "https"   = "30443"
      "monitor" = "30012"
    }
  }
  </code></pre>
  
  <p>There may be overlap between these; I want to select the value defined in the variable. (The named ports are the same for all instance groups.)</p>
  
  <p>First, I want to combine the two maps into a single map to make sure that there is one port for each name. How can I convert the first list of lists of maps to a single map so that I get this?</p>
  
  <pre><code>{
    "http"    = "30080"
    "https"   = "30443"
    "monitor" = "30012"
  }
  </code></pre>
  
  <p>Second, I want to convert all of that to the format needed on the command line:</p>
  
  <pre><code>gcloud --project ${var.project} compute instance-groups set-named-ports ${basename(var.backend[count.index])} --named-ports=https:443,http:30080,monitor:30012
  </code></pre>
  
  <p>I think I could do that with a <code>jsonencode</code> hack but I'd be interested in better solutions:</p>
  
  <pre><code>"${replace(jsonencode(named_ports), "/[\\{\\}\"\\s]/", "")}"
  </code></pre>