{"id":1791,"date":"2022-11-08T10:10:56","date_gmt":"2022-11-08T08:10:56","guid":{"rendered":"https:\/\/www.netexpertise.eu\/en\/?p=1791"},"modified":"2022-11-13T21:12:49","modified_gmt":"2022-11-13T19:12:49","slug":"turn-a-terraform-map-of-maps-into-a-single-map","status":"publish","type":"post","link":"http:\/\/www.netexpertise.eu\/en\/devops\/terraform\/turn-a-terraform-map-of-maps-into-a-single-map.html","title":{"rendered":"Turn a Terraform Map of Maps into a Single Map&#8230;"},"content":{"rendered":"\n<p>&#8230;Or how to avoid the <em>&#8216;Call to function &#8220;merge&#8221; failed: arguments must be maps or objects, got &#8220;tuple&#8221;.&#8217;<\/em> error message. Maps of maps example.<br><br><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"\/uploads\/maps-pages-papers-vintage-1854199-1024x682.jpg\" alt=\"maps pages papers vintage 1854199\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><br>Issue Producing a Single Map<\/h2>\n\n\n\n<p><br>Here&#8217;s a typical 2 level data structure that fits users in subgroups, themselves belonging to departments:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"vim\" class=\"language-vim\">locals {\n   groups = {\n      it = {\n         admin   = [\"it1\",\"it2\"]\n         editors = [\"it3\",\"it4\"]\n      },\n      accounts = {\n         editors = [\"account1\",\"account2\"]\n         viewers = [\"account3\",\"account4\"]\n      }\n   }\n}<\/code><\/pre>\n\n\n\n<p><br>There are good chances you will need to flatten that structure to create some <a href=\"\/en\/category\/devops\/terraform\">Terraform<\/a> ressources.<br>Our target is a map of unique subgroups department_subgroup = [ &#8220;user1&#8221;, &#8220;user2&#8221; ]. In our example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"vim\" class=\"language-vim\">subgroups = {\n  \"accounts_editors\" = [\n    \"account1\",\n    \"account2\",\n  ]\n  \"accounts_viewers\" = [\n    \"account3\",\n    \"account4\",\n  ]\n  \"it_admin\" = [\n    \"it1\",\n    \"it2\",\n  ]\n  \"it_editors\" = [\n    \"it3\",\n    \"it4\",\n  ]\n}<\/code><\/pre>\n\n\n\n<p><br>It is pretty simple to get a list of distinct maps flattening the resulting lists of a double loop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"vim\" class=\"language-vim\">subgroups = flatten([\n   for group,subgroups in local.groups : [\n      for subgroup, users in subgroups : {\n         \"${group}_${subgroup}\" = users\n      }\n   ]\n])\n# output:\nsubgroups = [\n  {\n    \"accounts_editors\" = [\n      \"account1\",\n      \"account2\",\n    ]\n  },\n  {\n    \"accounts_viewers\" = [\n      \"account3\",\n      \"account4\",\n    ]\n  },\n  {\n    \"it_admin\" = [\n      \"it1\",\n      \"it2\",\n    ]\n  },\n  {\n    \"it_editors\" = [\n      \"it3\",\n      \"it4\",\n    ]\n  },\n]<\/code><\/pre>\n\n\n\n<p>All we need is merge these maps into a single map but if we do, we end up with:<br><em>&#8216;Call to function &#8220;merge&#8221; failed: arguments must be maps or objects, got &#8220;tuple&#8221;.&#8217;<\/em><br><br><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"\/uploads\/merge_got_tuple-1024x384.png\" alt=\"Call to function &quot;merge&quot; failed: arguments must be maps or objects, got &quot;tuple&quot;\"\/><figcaption><em>Call to function &#8220;merge&#8221; failed: arguments must be maps or objects, got &#8220;tuple&#8221;<\/em><\/figcaption><\/figure>\n\n\n\n<p><br>2 ways to the rescue: the ugly and the elegant.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><br>The Ugly Way : Another Terraform Loop<\/h2>\n\n\n\n<p><br>The first way you can think of is to process the new map through another <a href=\"https:\/\/developer.hashicorp.com\/terraform\/language\/expressions\/for\" target=\"_blank\" rel=\"noreferrer noopener\">for<\/a> loop. That makes 3 loops and does not make any sense since a simple merge would do the job. Each map has one element only, so we take the first key and first value.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"vim\" class=\"language-vim\">subgroups = { for subgroup in flatten([\n   for group,subgroups in local.groups : [\n      for subgroup, users in subgroups : {\n         \"${group}_${subgroup}\" = users\n      }\n   ]\n]) : keys(subgroup)[0] =&gt; values(subgroup)[0] }<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><br>The Elegant Way : Function Expansion<\/h2>\n\n\n\n<p><br>This is much shorter than above solution:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"vim\" class=\"language-vim\">subgroups = merge(flatten([\n   for group,subgroups in local.groups : [\n      for subgroup, users in subgroups : {\n         \"${group}_${subgroup}\" = users\n      }\n   ]\n])...)<\/code><\/pre>\n\n\n\n<p>The result is exactly the same but what has changed? Just the 3 dots&#8230;<br><a href=\"https:\/\/developer.hashicorp.com\/terraform\/language\/expressions\/function-calls#expanding-function-arguments\" target=\"_blank\" rel=\"noreferrer noopener\">Expansion<\/a> takes each element out of a list and pass them on to the calling function.<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8230;Or how to avoid the &#8216;Call to function &#8220;merge&#8221; failed: arguments must be maps or objects, got &#8220;tuple&#8221;.&#8217; error message. Maps of maps example. Issue Producing a Single Map Here&#8217;s a typical 2 level data structure that fits users in subgroups, themselves belonging to departments: There are good chances you will need to flatten that [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[425,452],"tags":[453],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Netexpertise - Turn a Terraform Map of Maps into a Single Map...<\/title>\n<meta name=\"description\" content=\"Build Terraform single maps from multi-level maps with for loops and merge function expansion\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.netexpertise.eu\/en\/devops\/terraform\/turn-a-terraform-map-of-maps-into-a-single-map.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Netexpertise - Turn a Terraform Map of Maps into a Single Map...\" \/>\n<meta property=\"og:description\" content=\"Build Terraform single maps from multi-level maps with for loops and merge function expansion\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.netexpertise.eu\/en\/devops\/terraform\/turn-a-terraform-map-of-maps-into-a-single-map.html\" \/>\n<meta property=\"og:site_name\" content=\"Netexpertise\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-08T08:10:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-13T19:12:49+00:00\" \/>\n<meta name=\"author\" content=\"dave\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@netexpertise\" \/>\n<meta name=\"twitter:site\" content=\"@netexpertise\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/devops\/terraform\/turn-a-terraform-map-of-maps-into-a-single-map.html\",\"url\":\"https:\/\/www.netexpertise.eu\/en\/devops\/terraform\/turn-a-terraform-map-of-maps-into-a-single-map.html\",\"name\":\"Netexpertise - Turn a Terraform Map of Maps into a Single Map...\",\"isPartOf\":{\"@id\":\"http:\/\/www.netexpertise.eu\/en\/#website\"},\"datePublished\":\"2022-11-08T08:10:56+00:00\",\"dateModified\":\"2022-11-13T19:12:49+00:00\",\"author\":{\"@id\":\"http:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa\"},\"description\":\"Build Terraform single maps from multi-level maps with for loops and merge function expansion\",\"breadcrumb\":{\"@id\":\"https:\/\/www.netexpertise.eu\/en\/devops\/terraform\/turn-a-terraform-map-of-maps-into-a-single-map.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.netexpertise.eu\/en\/devops\/terraform\/turn-a-terraform-map-of-maps-into-a-single-map.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/devops\/terraform\/turn-a-terraform-map-of-maps-into-a-single-map.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/www.netexpertise.eu\/en\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Turn a Terraform Map of Maps into a Single Map&#8230;\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.netexpertise.eu\/en\/#website\",\"url\":\"http:\/\/www.netexpertise.eu\/en\/\",\"name\":\"Netexpertise\",\"description\":\"Systems \/ Networks \/ DevOps\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.netexpertise.eu\/en\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"http:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa\",\"name\":\"dave\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g\",\"caption\":\"dave\"},\"sameAs\":[\"http:\/\/www.netexpertise.eu\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Netexpertise - Turn a Terraform Map of Maps into a Single Map...","description":"Build Terraform single maps from multi-level maps with for loops and merge function expansion","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.netexpertise.eu\/en\/devops\/terraform\/turn-a-terraform-map-of-maps-into-a-single-map.html","og_locale":"en_US","og_type":"article","og_title":"Netexpertise - Turn a Terraform Map of Maps into a Single Map...","og_description":"Build Terraform single maps from multi-level maps with for loops and merge function expansion","og_url":"https:\/\/www.netexpertise.eu\/en\/devops\/terraform\/turn-a-terraform-map-of-maps-into-a-single-map.html","og_site_name":"Netexpertise","article_published_time":"2022-11-08T08:10:56+00:00","article_modified_time":"2022-11-13T19:12:49+00:00","author":"dave","twitter_card":"summary_large_image","twitter_creator":"@netexpertise","twitter_site":"@netexpertise","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.netexpertise.eu\/en\/devops\/terraform\/turn-a-terraform-map-of-maps-into-a-single-map.html","url":"https:\/\/www.netexpertise.eu\/en\/devops\/terraform\/turn-a-terraform-map-of-maps-into-a-single-map.html","name":"Netexpertise - Turn a Terraform Map of Maps into a Single Map...","isPartOf":{"@id":"http:\/\/www.netexpertise.eu\/en\/#website"},"datePublished":"2022-11-08T08:10:56+00:00","dateModified":"2022-11-13T19:12:49+00:00","author":{"@id":"http:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa"},"description":"Build Terraform single maps from multi-level maps with for loops and merge function expansion","breadcrumb":{"@id":"https:\/\/www.netexpertise.eu\/en\/devops\/terraform\/turn-a-terraform-map-of-maps-into-a-single-map.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.netexpertise.eu\/en\/devops\/terraform\/turn-a-terraform-map-of-maps-into-a-single-map.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.netexpertise.eu\/en\/devops\/terraform\/turn-a-terraform-map-of-maps-into-a-single-map.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/www.netexpertise.eu\/en"},{"@type":"ListItem","position":2,"name":"Turn a Terraform Map of Maps into a Single Map&#8230;"}]},{"@type":"WebSite","@id":"http:\/\/www.netexpertise.eu\/en\/#website","url":"http:\/\/www.netexpertise.eu\/en\/","name":"Netexpertise","description":"Systems \/ Networks \/ DevOps","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.netexpertise.eu\/en\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"http:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa","name":"dave","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/image\/","url":"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g","contentUrl":"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g","caption":"dave"},"sameAs":["http:\/\/www.netexpertise.eu"]}]}},"_links":{"self":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/posts\/1791"}],"collection":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/comments?post=1791"}],"version-history":[{"count":0,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/posts\/1791\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/media?parent=1791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/categories?post=1791"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/tags?post=1791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}