How to copy-past block into content of dynamic block

For a list

locals{
    x = [ {  yyy = { a=1 } }, {  yyy = { b=2 }  }   ]
}

I want to "copy-past" block yyy into content{} section of a dynamic block

   dynamic "test" {
      for_each = local.x
      content {
         zzz {x.yyy}
      }
   }

So the result should be

test{
  zzz {a=1}
}

test{
  zzz {b=2}
}

Is it possible without manually coping each field:

  content {
     zzz {
        a=ifnotnull(x.yyy.a)  
        b=ifnotnull(x.yyy.b)  
     }
  }

1 answer

  • answered 2022-01-19 19:40 tmatilai

    You'll need another dynamic for the inner block.

    But you must specify the attributes (the names) in the block. So assuming the possible attributes in the zzz block are a and b, something like this should work:

    dynamic "test" {
      for_each = local.x
    
      content {
        dynamic "zzz" {
          for_each = [test.value.yyy]
    
          content {
            a = lookup(zzz.value, "a", null)
            b = lookup(zzz.value, "b", null)
          }
        }
      }
    }
    

    Note that the yyy map is not needed, so if you control the structure of the input data, it could be just x = [ { a=1 }, { b=2 } ], and the inner iterator for_each = [test.value].

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum