title: Terraform missing map element
body: <p>Attempts to map the result from the SQL query. This seems correct to me, but terraform returns an error.</p>
  <p>It does not return any blank result for IP and server name.</p>
  <p>Is there a bug somewhere? It seems correct to me.</p>
  <p>I have main.tf</p>
  <pre><code>/*
   * admin database
   */
  
  module &quot;sql-servers-admindb&quot; {
    source = &quot;../terraform-modules/terraform-data-db&quot;
  
    dbtype = &quot;mysql&quot;
    connection = {
      mysql = {
        host  = local.mysql-admin_credentials.dbhost
        user  = local.mysql-admin_credentials.dbuser
        pass  = local.mysql-admin_credentials.dbpass
        dbase = local.mysql-admin_credentials.dbname
      }
      sqlite = null
    }
  
     debugfile = &quot;debug.json&quot;
  
   
    query = {
      sqlquery = &lt;&lt;EOT
  SELECT sd.serwer_id, sd.ip_local, sd.host_mysql
      FROM serwer_panel sd
      JOIN serwer_serwer_grupa ssg ON ssg.serwer_id = sd.serwer_id 
      JOIN serwer_grupa sg ON ssg.serwer_grupa_id = sg.id
  EOT
    }
  }
  
  
  module &quot;terraform-sql&quot; {
    source = &quot;../terraform-modules/terraform-sql&quot;
    depends_on = [module.sql-servers-admindb]
    for_each = { for sql in module.sql-servers-admindb.result.sqlquery : sql.serwer_id =&gt; sql }
    general = { 
       zone = &quot;sql.${var.dns_local_name[local.environment]}.&quot;
       ptr_networks = &quot;${var.dns_local_reverse_allowed[local.environment]}&quot;
    }
  }
  
  
  
  </code></pre>
  <p>terraform-modules/terraform-sql/main.tf</p>
  <pre><code>// IP address and A record
  module &quot;sql-nsrecord_a&quot; {
    source       = &quot;../terraform-modules/terraform-generic-nsrecord
    zone         = &quot;{var.general.zone}.&quot;
    name         = &quot;${var.sql.serwer_id}.&quot;
    type         = &quot;A&quot;
    records      = [var.sql.local_ip]
    ptr_networks = var.general.ptr_networks
  }
  
  
  </code></pre>
  <pre><code>
  /*
   * General
   */
  
  variable &quot;general&quot; {
    type = object({
      zone    = string
      ptr_networks = list(any)
    })
    default = {
      zone  = &quot;&quot;
      ptr_networks  = []
    }
  }
  
  
  /*
   * SQL records
   */
  
  variable &quot;sql&quot; {
    type    = map(any)
    default = {}
  }
  </code></pre>
  <p>Example debug.json</p>
  <pre><code>Array
  (
      [sqlquery] =&gt; Array
          (
              [0] =&gt; Array
                  (
                      [serwer_id] =&gt; s12
                      [ip_local] =&gt; 127.0.0.1
                      [host_mysql] =&gt; 
                  )
  </code></pre>
  <p>Return error:</p>
  <pre><code>│ Error: Missing map element
  │ 
  │   on ../terraform-modules/terraform-sql/main.tf line 9, in module &quot;sql-nsrecord_a&quot;:
  │    9:   name         = &quot;${var.sql.serwer_id}.&quot;
  │     ├────────────────
  │     │ var.sql is empty map of dynamic
  │ 
  │ This map does not have an element with the key &quot;serwer_id&quot;.
  ╵
  
  
  ╷
  │ Error: Missing map element
  │ 
  │   on ../terraform-modules/terraform-sql/main.tf line 11, in module &quot;sql-nsrecord_a&quot;:
  │   11:   records      = [var.sql.local_ip]
  │     ├────────────────
  │     │ var.sql is empty map of dynamic
  │ 
  </code></pre>
  <p>I am asking for advice on how to improve it.</p>