AdvancedLesson 9 of 16

Provision AKS with Terraform

Provision an AKS-ready Azure platform with Terraform and understand where Terraform stops and AKS, Helm, and CI/CD modules begin.

🧒 Simple Explanation (ELI5)

Terraform can build the land and utilities for your Kubernetes city: the network, cluster, identity, and container registry. Once that city exists, tools like Helm deploy the apps that live inside it.

🤔 Why Do We Need It?

🔧 Technical Explanation

A typical Terraform-driven AKS stack includes a resource group, subnet, managed identity or role assignments, a container registry, Log Analytics, and the AKS cluster. Terraform is responsible for provisioning the infrastructure. Application deployment is then handled through Helm and CI/CD.

hcl
resource "azurerm_kubernetes_cluster" "platform" {
  name                = "aks-platform-dev"
  location            = azurerm_resource_group.platform.location
  resource_group_name = azurerm_resource_group.platform.name
  dns_prefix          = "aksplatformdev"

  default_node_pool {
    name       = "system"
    node_count = 2
    vm_size    = "Standard_D4s_v5"
    vnet_subnet_id = azurerm_subnet.aks.id
  }

  identity {
    type = "SystemAssigned"
  }
}
LayerToolPurpose
InfrastructureTerraformCreate AKS and its Azure dependencies
Cluster PackagingHelmInstall and manage Kubernetes applications
Delivery WorkflowAzure DevOps / GitHub ActionsReview, plan, apply, and deploy changes
Terraform to AKS Toolchain
Terraform
AKS + ACR + Network
Helm
Applications
☸️
Boundary Clarity

Terraform should provision the cluster and its dependencies. Do not force it to manage every Kubernetes workload lifecycle. That is where Helm and deployment pipelines are better suited.

🌍 Real-World Use Case

A platform team uses Terraform to build a production AKS foundation in Azure. Azure DevOps generates the plan and applies approved changes. Once the cluster exists, Helm charts from the Helm course deploy ingress, monitoring, and application releases into the cluster.

🛠️ Hands-on

Add a Registry and Cluster Output

hcl
resource "azurerm_container_registry" "platform" {
  name                = "acrplatformdev001"
  resource_group_name = azurerm_resource_group.platform.name
  location            = azurerm_resource_group.platform.location
  sku                 = "Premium"
  admin_enabled       = false
}

output "aks_name" {
  value = azurerm_kubernetes_cluster.platform.name
}

🐛 Debugging Scenario

Problem: AKS provisioning fails even though the Terraform syntax is correct.

⚠️
AKS Reality

Many AKS errors are not Terraform-language problems. They are platform constraint or dependency problems surfaced during apply.

📋 Interview Questions

Beginner

Can Terraform provision AKS?

Yes. Terraform can provision the AKS cluster and its Azure dependencies such as networking and registry resources.

Why would a team use Terraform for AKS creation?

To keep cluster provisioning repeatable, versioned, and consistent across environments.

What tool would you use to deploy apps after Terraform creates AKS?

Usually Helm, often run through Azure DevOps or GitHub Actions pipelines.

What infrastructure often surrounds AKS?

VNets, subnets, role assignments, container registries, logging, and sometimes private DNS or ingress components.

Why expose AKS outputs from Terraform?

To provide downstream systems with cluster names, IDs, or related infrastructure values they need.

Intermediate

Why should Terraform and Helm responsibilities stay separate?

Because infrastructure lifecycle and application release lifecycle move at different speeds and require different operational patterns.

What Azure dependencies commonly cause AKS apply failures?

Subnets, quotas, identity permissions, role assignments, and registry integration are common causes.

Why is AKS a good example of module composition?

Because a cluster depends on reusable network, identity, monitoring, and registry modules rather than one giant flat file.

How should AKS infrastructure changes reach production?

Through plan review, approvals, and controlled CI/CD execution rather than ad hoc laptop applies.

How does this topic connect to the AKS course?

This lesson creates the infrastructure that the AKS course then explores operationally inside the cluster.

Scenario-Based

A teammate wants Terraform to deploy every app chart into AKS as part of cluster creation. What is your response?

I would separate cluster provisioning from application delivery. Terraform creates the platform, while Helm and CI/CD manage app releases.

AKS creation fails because subnet permissions are not ready yet. What likely helps?

Ensure required role assignments exist and, if necessary, model the dependency so provisioning waits for access to be established properly.

Why is a clean Terraform plan not enough to guarantee AKS success?

Because the apply step still depends on live Azure platform constraints such as quotas, regional availability, and dependency readiness.

How would you explain Terraform’s role in an AKS platform to leadership?

It standardizes cluster infrastructure creation so environments are consistent, reviewable, and easier to govern at scale.

How do Azure DevOps and Terraform complement each other here?

Azure DevOps runs the plan and apply workflow with approvals and auditability, while Terraform defines what AKS infrastructure should exist.

🧾 Summary

Terraform is excellent for provisioning AKS and its supporting Azure platform. Keep that responsibility clean, then hand off workload deployment to Helm and CI/CD for a scalable operating model.