In this blog, we are going to deploy an AKS cluster using Terraform. It is a very easy task. Before beginning this, you need to have Terraform installed in your system and an Azure account. Here I'm using the free tier of Azure and Windows OS.
![]() |
Terraform with AKS |
Terraform is installed on my local machine and it's good to use the PowerShell (not mandatory here).
Terraform is a free and open-source tool. Make sure its path is stored in a global variable and validate using this command.
terraform --version
To deploy the cluster we will use azurerm provider as it comes under azure resources. To deploy kubernetes services we use kuburnetes provider.
Make sure the file name is same as per now.
terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "3.59.0" } local = { source = "hashicorp/local" version = "2.4.0" } } } provider "azurerm" { subscription_id = "" client_id = "" client_secret = "" tenant_id = "" features { } }
resource "azurerm_resource_group" "aks" { name = "Tera-KGroup3" location = "East US" } resource "azurerm_kubernetes_cluster" "aks" { name = "terracluster3" location = azurerm_resource_group.aks.location resource_group_name = azurerm_resource_group.aks.name dns_prefix = "tradns3" default_node_pool { name = "default" node_count = 1 vm_size = "Standard_D2_v2" } identity { type = "SystemAssigned" } } resource "local_file" "kubeconfig" { content = azurerm_kubernetes_cluster.aks.kube_config_raw filename = "kubeconfig" }
Let's get the credential.
Login to your Azure account and follow below steps.
1. Here we get a client ID and tenant ID.
Path: Active Directory -> App registration(in left panel)->create/select app->overview
2. In that click Certificates and Secrets. You need to create a secret key and save its value as it is visible only once.
3. Now let's get the subscription id.
location: Search subscriptions->Choose an account that you would like to use -> Overview.
After getting all the credentials let's run the script. You can easily understand what a command means by its name.
terraform init terraform fmt terraform validate terraform plan terraform applyIt will show +,- a sign that represents what resources will be made and destroyed. Once validate the plan and then press yes. Wait till the command returns from execution.
Output will looks like this:
That's it. Hope you like it :)