Home > Net >  Attach an AKS Cluster to an existing VNET using Terraform
Attach an AKS Cluster to an existing VNET using Terraform

Time:12-11

I am a newbie in the DevOps and Terraform domain, and I would like to ask the following. I have already create a VNET (using portal) which called "myVNET" in the resource group "Networks". I am trying to implement a AKS cluster using terraform, my main.tf file is belolw

provider "azurerm" {
  subscription_id = var.subscription_id
  client_id       = var.client_id
  client_secret   = var.client_secret
  tenant_id       = var.tenant_id
features {}
}
resource "azurerm_resource_group" "MyPlatform" {
  name     = var.resourcename
  location = var.location
}
resource "azurerm_kubernetes_cluster" "aks-cluster" {
  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       = "default"
    node_count = var.agentnode
    vm_size    = var.size
  }
service_principal {
    client_id     = var.client_id
    client_secret = var.client_secret
  }

network_profile {
    network_plugin     = "azure"
    load_balancer_sku  = "standard"
    network_policy     = "calico"
  }
}

My question is the following, how can I attach my cluster to my VNET?

Thank you in advance.

CodePudding user response:

You can use data source azurerm_virtual_network

Learn more about data sources

I hope this answers your question. :)

CodePudding user response:

You do that by assigning the subnet ID to the node pool vnet_subnet_id.

data "azurerm_subnet" "subnet" {
  name                 = "<name of the subnet to run in>"
  virtual_network_name = "MyVNET"
  resource_group_name  = "Networks"
}
...
resource "azurerm_kubernetes_cluster" "aks-cluster" {
...
    default_node_pool {
      name             = "default"
      ...
      vnet_subnet_id   = data.azurerm_subnet.subnet.id
    }
...

You can reference this existing module to build your own module if not use it directly.

  • Related