<p>I am new to the DevOps and Terraform domain, and I would like to ask the following. I have already create a VNET (using portal) which called &quot;myVNET&quot; in the resource group &quot;Networks&quot;. I am trying to implement a AKS cluster using Terraform. My main.tf file is below</p>
<pre><code>provider &quot;azurerm&quot; {
  subscription_id = var.subscription_id
  client_id       = var.client_id
  client_secret   = var.client_secret
  tenant_id       = var.tenant_id
features {}
}
resource &quot;azurerm_resource_group&quot; &quot;MyPlatform&quot; {
  name     = var.resourcename
  location = var.location
}
resource &quot;azurerm_kubernetes_cluster&quot; &quot;aks-cluster&quot; {
  name                = var.clustername
  location            = azurerm_resource_group.MyPlatform.location
  resource_group_name = azurerm_resource_group.MyPlatform.name
  dns_prefix          = var.dnspreffix
default_node_pool {
    name       = &quot;default&quot;
    node_count = var.agentnode
    vm_size    = var.size
  }
service_principal {
    client_id     = var.client_id
    client_secret = var.client_secret
  }

network_profile {
    network_plugin     = &quot;azure&quot;
    load_balancer_sku  = &quot;standard&quot;
    network_policy     = &quot;calico&quot;
  }
}

</code></pre>
<p>My question is the following, how can I attach my cluster to my VNET?</p>

p4pe
<p>You do that by assigning the subnet ID to the node pool <a href="https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/kubernetes_cluster#vnet_subnet_id" rel="nofollow noreferrer">vnet_subnet_id</a>.</p>
<pre><code>data &quot;azurerm_subnet&quot; &quot;subnet&quot; {
  name                 = &quot;&lt;name of the subnet to run in&gt;&quot;
  virtual_network_name = &quot;MyVNET&quot;
  resource_group_name  = &quot;Networks&quot;
}
...
resource &quot;azurerm_kubernetes_cluster&quot; &quot;aks-cluster&quot; {
...
    default_node_pool {
      name             = &quot;default&quot;
      ...
      vnet_subnet_id   = data.azurerm_subnet.subnet.id
    }
...
</code></pre>
<p>You can reference this <a href="https://github.com/edalferes/terraform-azure-aks" rel="nofollow noreferrer">existing module</a> to build your own module if not use it directly.</p>
