<p>I am creating a kube cluster with GKE in terraform. I am creating the cluster from two modules, a cluster module and a nodepool module. I'd like to create a module for the master_authorized_networks_config so that each time a new cidr is added to it terraform doesn't destroy the original cluster. Is this possible. This is an example of the block of code that's in my cluster module that I would like to change without destroying the whole cluster. These IP addresses are what allow acess to the cluster.</p>
<pre><code>master_authorized_networks_config {
        cidr_blocks {
            cidr_block   = &quot;123.456.789/32&quot;
            display_name = &quot;megacorp-1-nat1&quot;
        }
        cidr_blocks {
            cidr_block   = &quot;34.69.69.69/32&quot;
            display_name = &quot;megacorp-1-nat2&quot;
        }
        cidr_blocks {
            cidr_block   = &quot;123.456.333.333/32&quot;
            display_name = &quot;vpn-test&quot;
        }
</code></pre>
<p>Adding another cidr destroys the original cluster. I don't want this.</p>

TeeTee
<p>You can use <a href="https://developer.hashicorp.com/terraform/language/expressions/dynamic-blocks" rel="nofollow noreferrer">dynamic blocks</a> to achieve this.
In your terraform template, write:</p>
<pre><code>master_authorized_networks_config {
dynamic &quot;cidr_blocks&quot; {
  for_each = var.authorized_networks
  content {
    cidr_block = cidr_blocks.key
    display_name = cidr_blocks.value
  }
}
</code></pre>
<p>Then you can put the allowed ip ranges in a variable of type map(string).
Example:</p>
<pre><code>authorized_networks= {
  &quot;14.170.140.29/32&quot;: &quot;home&quot;,
  &quot;181.72.169.20/32&quot;: &quot;office&quot;,
  &quot;181.73.147.199/32&quot;: &quot;office Wi-Fi&quot;
}
</code></pre>
