title: Terraform dynamic inside dynamic
body: <p>The idea is to generate something like this:</p>
  <pre><code>  environment {
      ...
  
      environment_variable {
        ...
      }
  
      environment_variable {
        ...
      }
    }
  </code></pre>
  <p>Where there is a environment block with some properties and 0...n environment_variables with some properties.</p>
  <p>I am having the following module which uses a dynamic inside a dynamic. Is this possible? Or how would I achieve this?</p>
  <p>What i have so far is the following module which uses a dynamic inside a dynamic:</p>
  <p>module:</p>
  <pre><code>dynamic &quot;environment&quot; {
    for_each = var.environment_definition
  
    content {
      field1 = environment.value.field1
      field2 = environment.value.field2
  
      dynamic &quot;environment_variable&quot; {
        for_each = length(var.environment_variables) == 0 ? [] : [var.environment_variables]
  
        content {
          name  = environment_variable.value.env_name
          value = environment_variable.value.env_value
        }
      }
    }
  }
  </code></pre>
  <p>variables.tf:</p>
  <pre><code>variable &quot;environment_definition&quot; {
    description = &quot;&quot;
    type = any
    default = {}
  }
  
  variable &quot;environment_variables&quot; {
    description = &quot;&quot;
    type = map(object({
      env_name  = string
      env_value = string
    }))
    default = {}
  }
  </code></pre>
  <p>And the above module with its variables is called in my main.tf:</p>
  <pre><code>  environment_definition = {
      field1                = &quot;value&quot;
      field2                = &quot;value&quot;
    }
  
    environment_variables = {
      tf_version = {
        env_name  = &quot;TERRAFORM_VERSION&quot;
        env_value = &quot;0.12.28&quot;
      },
      aws_account = {
        env_name  = &quot;AWS_ACCOUNT&quot;
        env_value = &quot;Account123456&quot;
      },
      environment = {
        env_name  = &quot;ENVIRONMENT&quot;
        env_value = &quot;dev&quot;
      }
    }
  </code></pre>
  <p>However I am getting this error:</p>
  <pre><code>Error: Missing map element
  
    on ../../../modules/codebuild/main.tf line 123, in resource &quot;aws_codebuild_project&quot; &quot;this&quot;:
   152:           value = environment_variable.value.env_value
      |----------------
      | environment_variable.value is map of object with 3 elements
  
  This map does not have an element with the key &quot;env_value&quot;.
  </code></pre>
  <p>How can I successfully implement this?</p>