p4pe
p4pe

Reputation: 15

Attach an AKS Cluster to an existing VNET using Terraform

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 "myVNET" in the resource group "Networks". I am trying to implement a AKS cluster using Terraform. My main.tf file is below

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?

Upvotes: 0

Views: 2236

Answers (1)

gohm'c
gohm'c

Reputation: 15530

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.

Upvotes: 2

Related Questions