title: Iterating through a map of objects with multiple values using for_each
body: <p>I have multiple DNS records that I am creating using Terraform. One of my records has two values that it needs to read from. I am having an issue getting Terraform to be able to loop through my variables in order for this to read successfully. Below is my code. I am getting an error that does not make any sense even though I have the correct data type. There is some error with how I am looping through these resources, and I am having some trouble figuring out where I went wrong. Any advice would be appreciated.</p>
  <pre><code>variables.tf
  </code></pre>
  <pre><code>    variable &quot;mx&quot; {
        type = map(object({
          ttl     = string
          records = set(string)
        }))
      }
  </code></pre>
  <pre><code>variables.tfvars
  </code></pre>
  <pre><code>  mx = {
         &quot;mx_record1&quot; = {
           ttl = &quot;3600&quot;
           records = [
             &quot;mx_record1_value&quot;
           ]
         }
         &quot;mx_record2&quot; = {
           ttl = &quot;3600&quot;
           records = [
             &quot;mx_record2_value&quot;
             &quot;mx_record2_value2&quot;
           ]
         }
  </code></pre>
  <pre><code>mx.tf
  </code></pre>
  <pre><code>  locals {
         mx_records = flatten([
           for mx_key, mx in var.mx : [
             for record in mx.records : {
               record = record
               mx     = mx_key
             }
           ]
         ])
       }
   
   resource &quot;aws_route53_record&quot; &quot;mx_records&quot; {
     for_each = { for mx in local.mx_records : mx.record =&gt; mx }
     zone_id  = aws_route53_zone.zone.zone_id
     name     = each.key
     type     = &quot;MX&quot;
     ttl      = each.value.ttl
   
     records = [
       each.value.record
     ]
   }
  </code></pre>
  <p>Got below error</p>
  <pre><code>    Error:
      
          Error: Unsupported attribute
      
        on mx.tf line 17, in resource &quot;aws_route53_record&quot; &quot;mx_records&quot;:
        17:   ttl      = each.value.ttl
          |----------------
          | each.value is object with 2 attributes
      
      This object does not have an attribute named &quot;ttl&quot;.
  </code></pre>
  <p>UPDATE:</p>
  <pre><code>
      locals {
    mx_records = flatten([
      for mx_key, mx in var.mx : [
        for record in mx.records : {
          record = record
          mx     = mx_key
          ttl    = mx.ttl
        }
      ]
    ])
  }
  
  resource &quot;aws_route53_record&quot; &quot;mx_records&quot; {
    for_each = { for mx in local.mx_records : mx.record =&gt; mx }
    zone_id  = aws_route53_zone.zone.zone_id
    name     = each.key
    type     = &quot;MX&quot;
    ttl      = each.value.ttl
  
    records = [
      each.value.record
    ]
  }
  </code></pre>
  <p>ERROR</p>
  <pre><code>
      Error: [ERR]: Error building changeset: InvalidChangeBatch: [FATAL problem: UnsupportedCharacter (Value contains unsupported characters) encountered with ' ']
          status code: 400, request id: a27e6a47-c10f-42ce-be94-10aaa9c276f8
  
    on mx.tf line 13, in resource &quot;aws_route53_record&quot; &quot;mx_records&quot;:
    13: resource &quot;aws_route53_record&quot; &quot;mx_records&quot; {
  </code></pre>