CodexBloom - Programming Q&A Platform

Terraform errors with Azure Kubernetes Service provisioning due to network configuration

👀 Views: 1 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-03
terraform azure kubernetes HCL

I'm relatively new to this, so bear with me... I'm testing a new approach and I'm deploying to production and I've looked through the documentation and I'm still confused about I'm currently trying to provision an Azure Kubernetes Service (AKS) cluster using Terraform, but I'm working with a strange scenario when it comes to the network configuration... Despite following the Azure documentation closely, I'm getting the behavior: `"behavior: creating AKS cluster: behavior creating agent pool: Code="InvalidParameter" Message="Agent pool subnet must belong to the configured virtual network."`. I've defined my virtual network and subnet in the following way: ```hcl resource "azurerm_virtual_network" "main" { name = "myVnet" address_space = ["10.0.0.0/16"] location = "East US" resource_group_name = azurerm_resource_group.main.name } resource "azurerm_subnet" "aks_subnet" { name = "aksSubnet" resource_group_name = azurerm_resource_group.main.name virtual_network_name = azurerm_virtual_network.main.name address_prefixes = ["10.0.1.0/24"] } ``` And here is how I'm attempting to create the AKS cluster: ```hcl resource "azurerm_kubernetes_cluster" "aks" { name = "myAKSCluster" location = azurerm_resource_group.main.location resource_group_name = azurerm_resource_group.main.name dns_prefix = "myaks" default_node_pool { name = "default" node_count = 3 vm_size = "Standard_DS2_v2" availability_zones = ["1", "2", "3"] subnet_id = azurerm_subnet.aks_subnet.id } identity { type = "SystemAssigned" } } ``` I double-checked that the subnet ID is being passed correctly and that the subnet is indeed part of the virtual network, but the behavior continues. I've also ensured that the address space of the subnet does not overlap with any existing resources. Any ideas on what might be going wrong? Could it be an scenario with the Azure API or some quirks in the Terraform version (I'm using 1.4.0)? Additionally, I'm using the provider version `3.0.0` for Azure. I've tried running `terraform plan` and `terraform apply` multiple times, but the behavior continues to show up. Thanks in advance for your help! Am I missing something obvious?