Deploying and managing Kubernetes clusters on Azure (AKS) can be quite difficult. So, Infrastructure as Code (IaC) tools like Terraform can let you automate the entire deployment, which will make it even more efficient, repeatable, and less error-prone. This post will show you how to get the AKS Database created by Terraform instead.

Prerequisites

Before you begin, make sure you have the following.

Setting up the Terraform Configuration

Here's the complete main.tf file.

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.0" # Or the latest version
    }
  }
}

provider "azurerm" {
  features {}
  # The provider block is used to configure the Azure provider.
  # The 'features' block is required for Terraform 3.0 and later.
  # Authentication is handled outside the Terraform configuration,
  # typically using the Azure CLI (az login).
}

resource "azurerm_resource_group" "aks_rg" {
  name     = "aks-resource-group" # Replace with your desired resource group name
  location = "East US"             # Replace with your desired Azure region
}

resource "azurerm_kubernetes_cluster" "aks_cluster" {
  name                = "my-aks-cluster" # Replace with your desired cluster name
  location            = azurerm_resource_group.aks_rg.location
  resource_group_name = azurerm_resource_group.aks_rg.name
  dns_prefix          = "myakscluster" # Replace with your desired DNS prefix

  default_node_pool {
    name           = "default"
    node_count     = 3             # Number of nodes in the default node pool
    vm_size        = "Standard_DS2_v2" # Size of the virtual machines
    os_disk_size_gb = 30
  }

  identity {
    type = "SystemAssigned"
  }

  tags = {
    Environment = "Production"
  }
}

output "kube_config" {
  value     = azurerm_kubernetes_cluster.aks_cluster.kube_config_raw
  sensitive = true #  Mark the output as sensitive
}

Deploying the AKS Cluster

Conclusion

Terraform helps to automate the creation and deployment of AKS very easily. Some of the benefits from it are an increase in efficiency, improved consistency, reduced errors and automated infrastructure. You can also modify the networking, and scaling of pods as per your requirements.

Hope you learned something new today, feel free to add your feedback in the comments.