<p>I am trying to pass key, value pair to a resource and using the key as an attribute and its value as attributes value. But it throws an error message</p>
<pre><code>each.key = each.value

An argument or block definition is required here. To set an argument, use the
equals sign &quot;=&quot; to introduce the argument value.
</code></pre>
<p>This is what I am trying to do</p>
<pre><code>variable &quot;k8s_labels&quot; {
  description = &quot;Kubernetes labels to set for the node pool.&quot;
  type = map
  default = {
    label1 = &quot;value1&quot;,
    label2 = &quot;value2&quot;,
  }
}

resource &quot;google_container_node_pool&quot; &quot;default&quot; {
  .
  .
  .
  dynamic &quot;labels&quot; {
    for_each = var.k8s_labels
    content {
      each.key = each.value
    }
  }
}

</code></pre>

<p>In a dynamic content block, the name of the temporary lambda iterator variable is not <code>each</code>, but rather the name of the block itself. In this case, that would be <code>labels</code>. Additionally, the labels block must be enclosed within a <code>node_config</code> block according to the <a href="https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/container_cluster#labels" rel="nofollow noreferrer">schema documentation</a>. These fixes in combination will resolve your issue.</p>
<p>You can update your dynamic content block like:</p>
<pre><code>resource &quot;google_container_node_pool&quot; &quot;default&quot; {
  ...

  node_config {
    dynamic &quot;labels&quot; {
      for_each = var.k8s_labels

      content {
        labels.key = labels.value
      }
    }
  }
}
</code></pre>
<p>This helps to distinguish the temporary lambda iterator variable within the scope of the dynamic block from one within the scope of the resource itself. If iterating within the scope of the resource:</p>
<pre><code>resource &quot;google_container_node_pool&quot; &quot;default&quot; {
  for_each = var.foo # var.foo of type Map
}
</code></pre>
<p>then those key-value pairs would be accessible within the dynamic content blocks also with <code>each.key</code> and <code>each.value</code>. This avoids variable naming collision within the scope of the dynamic content block.</p>
<p>You can check the <a href="https://www.terraform.io/docs/configuration/expressions/dynamic-blocks.html" rel="nofollow noreferrer">dynamic block documentation</a> for more information, including how to rename the variable with the <code>iterator</code> argument.</p>
