<p>I'm trying to add a dynamic node pool but I'm getting a few errors.</p>
<p>main.tf</p>
<pre><code>resource &quot;digitalocean_kubernetes_cluster&quot; &quot;example_cluster&quot; {
  name    = var.name
  region  = var.region
  version = &quot;1.24.12-do.0&quot;

  dynamic &quot;node_pool&quot; {
    for_each = var.node_pools
    content {
      name       = node_pool.value.name
      size       = node_pool.value.size
      node_count = node_pool.value.node_count
      labels     = node_pool.value.labels
      auto_scale = &quot;true&quot;
      #min_nodes  = &quot;1&quot;
      #max_nodes  = &quot;15&quot;
      #count = node_pool.value.auto_scale ? 1 : 0
     
      dynamic &quot;auto_scale&quot; {
        for_each = node_pool.value.auto_scale ? [node_pool.value.auto_scale] : []
        content {
          min_nodes = node_pool.value.min_nodes
          max_nodes = node_pool.value.max_nodes

        }
      } 
    }
  }
</code></pre>
<p>veriables.tf</p>
<pre><code>variable &quot;name&quot; {
  type        = string
  description = &quot;The name of the Kubernetes cluster to be created.&quot;
}

variable &quot;region&quot; {
  type        = string
  description = &quot;The region in which to create the Kubernetes cluster.&quot;
}

#variable &quot;version&quot; {
 # description = &quot;Version of the Kubernetes cluster&quot;
#}

variable &quot;node_pools&quot; {
  description = &quot;List of node pools&quot;
  type = list(object({
    name       = string
    size       = string
    node_count = number
    min_nodes  = number
    max_nodes  = number
    auto_scale = bool
    labels     = map(string)
  }))
}
</code></pre>
<p>and my app.tf</p>
<pre><code>module &quot;kubernetes&quot; {
  source = &quot;../modules/kubernetes&quot;

  name   = &quot;test-cluster&quot;
  region = &quot;fra1&quot;
  # version = &quot;1.24.12-do.0&quot;
  node_pools = [
    {
      name       = &quot;pool-test1-8c-16gb-tests&quot;
      size       = &quot;s-8vcpu-16gb&quot;
      node_count = 2
      auto_scale = &quot;true&quot;
      min_nodes  = 1
      max_nodes  = 15
      labels = {
        runner = &quot;test1&quot;
      }
    },
    {
      name       = &quot;pool-test2-4c-8gb&quot;
      size       = &quot;s-4vcpu-8gb&quot;
      node_count = 1
      auto_scale = &quot;true&quot;
      min_nodes  = 1
      max_nodes  = 15
      labels = {
        runner = &quot;test2&quot;
      }
    }
  ]
}
</code></pre>
<p>the error I'm getting is</p>
<blockquote>
<p>│ Error: Unsupported block type
│
│   on ../modules/DO/kubernetes/main.tf line 32, in resource &quot;digitalocean_kubernetes_cluster&quot; &quot;example_cluster&quot;:
│   32:       dynamic &quot;auto_scale&quot; {
│
│ Blocks of type &quot;auto_scale&quot; are not expected here.</p>
</blockquote>
<p>if I hardcode the auto_scale I'm getting this</p>
<blockquote>
<p>╷
│ Error: Too many node_pool blocks
│
│   on ../modules/DO/kubernetes/main.tf line 22, in resource &quot;digitalocean_kubernetes_cluster&quot; &quot;example_cluster&quot;:
│   22:     content {
│
│ No more than 1 &quot;node_pool&quot; blocks are allowed
╵</p>
</blockquote>
<p>can someone point out what am I doing wrong?</p>

Debugger
<p>The DO provider does not allow for more than one <a href="https://registry.terraform.io/providers/digitalocean/digitalocean/latest/docs/resources/kubernetes_cluster#node_pool" rel="nofollow noreferrer"><code>node_pool</code> block</a>:</p>
<blockquote>
<p>(Required) A block representing the cluster's default node pool. Additional node pools may be added to the cluster using the <code>digitalocean_kubernetes_node_pool</code> resource.</p>
</blockquote>
<p>Furthermore, the <code>auto_scale</code> option is not a block, rather an argument which should have a boolean value:</p>
<pre><code>resource &quot;digitalocean_kubernetes_cluster&quot; &quot;foo&quot; {
  name    = &quot;foo&quot;
  region  = &quot;nyc1&quot;
  version = &quot;1.22.8-do.1&quot;

  node_pool {
    name       = &quot;autoscale-worker-pool&quot;
    size       = &quot;s-2vcpu-2gb&quot;
    auto_scale = true
    min_nodes  = 1
    max_nodes  = 5
  }
}
</code></pre>
<p>This <a href="https://registry.terraform.io/providers/digitalocean/digitalocean/latest/docs/resources/kubernetes_cluster#autoscaling-example" rel="nofollow noreferrer">example</a> is taken from the resource documentation.</p>
<p>Here is how I would approach the refactoring:</p>
<ol>
<li>Define two variables, one for default node group, one for additional node groups</li>
<li>Use the <code>digitalocean_kubernetes_node_pool</code> resource for additional node pools</li>
</ol>
<p>Variables would then look like the following:</p>
<pre><code>variable &quot;default_node_pool&quot; {
  type = object({
    name       = string
    size       = string
    node_count = number
    min_nodes  = number
    max_nodes  = number
    auto_scale = bool
    labels     = map(string)
    }
  )
  description = &quot;Default node pool.&quot;

  default = {
    name       = &quot;pool-test1-8c-16gb-tests&quot;
    size       = &quot;s-8vcpu-16gb&quot;
    node_count = 2
    auto_scale = true
    min_nodes  = 1
    max_nodes  = 15
    labels = {
      runner = &quot;test1&quot;
    }
  }
}

variable &quot;additional_node_pools&quot; {
  description = &quot;Additional node pools.&quot;
  type = map(object({
    name       = string
    size       = string
    node_count = number
    min_nodes  = number
    max_nodes  = number
    auto_scale = bool
    labels     = map(string)
    }
  ))

  default = {
    &quot;test1&quot; = {
      name       = &quot;pool-test2-4c-8gb&quot;
      size       = &quot;s-4vcpu-8gb&quot;
      node_count = 1
      auto_scale = true
      min_nodes  = 1
      max_nodes  = 15
      labels = {
        runner = &quot;test2&quot;
      }
    }
  }
}
</code></pre>
<p>Then, in the module code, you would change to the following:</p>
<pre><code>resource &quot;digitalocean_kubernetes_cluster&quot; &quot;example_cluster&quot; {
  name    = var.name
  region  = var.region
  version = &quot;1.24.12-do.0&quot;

  node_pool {
    name       = var.default_node_pool.name
    size       = var.default_node_pool.size
    node_count = var.default_node_pool.node_count
    labels     = var.default_node_pool.labels
    auto_scale = var.default_node_pool.auto_scale
    max_nodes  = var.default_node_pool.max_nodes
    min_nodes  = var.default_node_pool.min_nodes
  }
}

resource &quot;digitalocean_kubernetes_node_pool&quot; &quot;bar&quot; {
  for_each   = var.additional_node_pools
  cluster_id = digitalocean_kubernetes_cluster.example_cluster.id

  name       = each.value.name
  size       = each.value.size
  node_count = each.value.node_count
  min_nodes  = each.value.min_nodes
  max_nodes  = each.value.max_nodes
  auto_scale = each.value.auto_scale

  labels = each.value.labels
}
</code></pre>
<p>When calling the module you would of course have to adjust how you are passing the variables according to the module definition:</p>
<pre><code>module &quot;kubernetes&quot; {
  source = &quot;../modules/kubernetes&quot;


  name   = &quot;test-cluster&quot;
  region = &quot;fra1&quot;
  # version = &quot;1.24.12-do.0&quot;
  default_node_pool = {
    name       = &quot;pool-test1-8c-16gb-tests&quot;
    size       = &quot;s-8vcpu-16gb&quot;
    node_count = 2
    auto_scale = true
    min_nodes  = 1
    max_nodes  = 15
    labels = {
      runner = &quot;test1&quot;
    }
  }
  additional_node_pools = {
    &quot;test1&quot; = {
      name       = &quot;pool-test2-4c-8gb&quot;
      size       = &quot;s-4vcpu-8gb&quot;
      node_count = 1
      auto_scale = true
      min_nodes  = 1
      max_nodes  = 15
      labels = {
        runner = &quot;test2&quot;
      }
    }
  }
}
</code></pre>
