Open In App

How To Create AKS Cluster In Azure Using Terraform ?

Last Updated : 26 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Azure AKS also called as Azure Kubernetes Service is service that provides Kubernetes implementation in the Azure cloud. Azure Kubernetes service is a managed service that allows deployment of containerized applications in azure cloud. Setting up a Kubernetes cluster in azure is tedious task so lets see how we can set up AKS using terraform.

Understanding Of Primary Terminologies

The following are the primary components of Azure AKS related with Terraform:

Setup Azure AKS Using Terraform: A Step-By-Step Guide

Step 1: Set Up Terraform

  • Download the Terraform zip from the installation page of the Terraform website.
  • Extract and paste the terraform folder to the required location and add the path to runnable in environment variables.

Step 2: Set Up Azure CLI

  • Download the Azure CLI setup from the official website.
  • Run the installer and follow the steps to install.

Azure CLI

Step 3: Configure Azure CLI

  • Open terminal and run below command.
az login
  • A browser window will open for login. Login with your azure credentials. Once it is done you will see output as below.

Configuring Azure CLI

Step 4: Create Terraform Code

  • Goto your project folder and create main.tf file.
  • Add terraform block to code with azure as required provider with latest version. You can find the latest version at hashicorp registry.
  • Terraform block should look like below. You can add required version to avoid invalidation.
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
}
}
  • Now add provider as azurerm like below. Specify other details as required.
provider "azurerm" {
features {}
}
  • Add configuration for AKS cluster. For this article we will add only required arguments. You can specify other arguments if required.
resource "azurerm_kubernetes_cluster" "gfg_aks" {
name = "gfg-aks"
location = "eastus"
resource_group_name = "DeepsLab"
dns_prefix = "gfgaks"

default_node_pool {
name = "default"
node_count = 1
vm_size = "Standard_D2_v2"
}

identity {
type = "SystemAssigned"
}
}
  • We have specified name for AKS cluster. We have specified the location and resource group where cluster should be created.
  • The node pool must be specified for the cluster. For this we have added one pool with 1 node of size D2_v2.
  • we have added system assigned identity and dns prefix for dns name.
  • The complete code will look like below.
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
}
}

provider "azurerm" {
features {}
}

resource "azurerm_kubernetes_cluster" "gfg_aks" {
name = "gfg-aks"
location = "eastus"
resource_group_name = "DeepsLab"
dns_prefix = "gfgaks"

default_node_pool {
name = "default"
node_count = 1
vm_size = "Standard_D2_v2"
}

identity {
type = "SystemAssigned"
}
}

Step 5: Apply The Terraform Code

  • Once the code is ready you can apply it.
  • First init the terraform by running below command in project folder where main.tf is present.
terraform init

Terraform init

  • After successful output of terraform apply the changes using below command.
terrraform apply
  • After verifying type “yes” to confirm and apply.

Terraform Apply

  • Terraform will start creating cluster.

Validating Yes

  • You can also verify deployment by visiting Kubernetes services page of Azure.

Kubernetes Servies In Azure

Conclusion

We have successfully deployed an Azure AKS cluster with the help of terraform in this article. the configuration described can be further modified to make changes to the node pool of cluster in azure. This is how terraform allows reusable and modifiable configuration of infrastructure.

AKS Cluster In Azure Using Terraform – FAQ’s

Why Use Terraform To Create AKS Clusters?

Terraform enables infrastructure automation and ensures consistency across environments. Using Terraform to create AKS clusters allows you to define the cluster configuration as code, making it easier to manage and reproduce.

What Resources Are Required To Create An AKS Cluster With Terraform?

To create an AKS cluster with Terraform, you need an Azure subscription, a resource group to contain the cluster resources, and credentials with the necessary permissions to create resources in Azure.

What Are The Main Steps To Create An AKS Cluster Using Terraform?

The main steps to create an AKS cluster using Terraform include defining the cluster configuration in a Terraform configuration file (typically with a .tf extension), initializing Terraform, applying the configuration to create the resources, and verifying the cluster creation.

What Parameters Can I Configure For An Aks Cluster With Terraform?

With Terraform, you can configure various parameters for an AKS cluster, including the Kubernetes version, node size and count, network configuration, authentication method, monitoring, and logging settings.

Can I Use Terraform Modules To Create Aks Clusters?

Yes, Terraform modules provide a way to encapsulate and reuse infrastructure configurations. You can use existing modules or create your own to simplify the creation of AKS clusters with Terraform.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads