๐ Day 7: Deploy NGINX on AKS with Terraform: Full Guide with LoadBalancer Access


In this post, you'll learn how to:
Create an AKS (Azure Kubernetes Service) cluster using Terraform
Deploy an NGINX web server
Expose it with a Kubernetes LoadBalancer
Access the NGINX welcome page via public IP
๐ฆ Prerequisites
Before we begin, make sure you have the following installed:
Terraform
Azure CLI
kubectl
An active Azure subscription
Login to Azure:
az login
Creating a Service Principal on Azure
If you want to automate some of the workloads on Azure, then you will need Service Principal (SP) accounts. We will use an SP for our automation.
Because only way for Terraform to work on Azure is to connecting it, we will connect Terraform with Azure via a Service Principal. Letโs create our SP.
NOTE: If youโve followed my previous post and created your Service Principal, you can use it in this example too.
You will need your subscription ID for this SP. You can get your subscription ID after logging into Azure-CLI or Azure portal and using CLI with below command:
az account show --subscription <subscription_name> --query id
After getting your subscription ID, we can create our SP with below command:
az ad sp create-for-rbac - name <service_principal_name> - role Contributor - scopes /subscriptions/<subscrip
๐ ๏ธ Project Structure
Create your project folder and files:
mkdir -p aks-nginx && cd aks-nginx
touch main.tf variables.tf terraform.tfvars outputs.tf
Final structure:
aks-nginx/
โโโ main.tf
โโโ variables.tf
โโโ terraform.tfvars
โโโ outputs.tf
๐น variables.tf
Define all input variables here:
variable "resource_group_name" {
description = "Name of the resource group"
type = string
}
variable "location" {
description = "Azure region"
type = string
default = "East US"
}
variable "aks_cluster_name" {
description = "AKS cluster name"
type = string
}
variable "dns_prefix" {
description = "DNS prefix for AKS cluster"
type = string
}
variable "node_count" {
description = "Number of nodes in the default pool"
type = number
default = 1
}
๐น terraform.tfvars
Provide actual values here:
resource_group_name = "aks-nginx-rg"
location = "centralindia"
aks_cluster_name = "aks-nginx-cluster"
dns_prefix = "aksnginx"
node_count = 1
๐น main.tf
Hereโs the complete configuration to:
Create resource group and AKS cluster
Set up Kubernetes provider
Deploy NGINX Deployment
Expose it using LoadBalancer service
provider "azurerm" {
features {}
}
provider "kubernetes" {
host = azurerm_kubernetes_cluster.aks.kube_config[0].host
client_certificate = base64decode(azurerm_kubernetes_cluster.aks.kube_config[0].client_certificate)
client_key = base64decode(azurerm_kubernetes_cluster.aks.kube_config[0].client_key)
cluster_ca_certificate = base64decode(azurerm_kubernetes_cluster.aks.kube_config[0].cluster_ca_certificate)
}
resource "azurerm_resource_group" "aks" {
name = var.resource_group_name
location = var.location
}
resource "azurerm_kubernetes_cluster" "aks" {
name = var.aks_cluster_name
location = azurerm_resource_group.aks.location
resource_group_name = azurerm_resource_group.aks.name
dns_prefix = var.dns_prefix
default_node_pool {
name = "default"
node_count = var.node_count
vm_size = "Standard_D2ps_v5"
}
identity {
type = "SystemAssigned"
}
tags = {
environment = "Dev"
}
}
resource "kubernetes_deployment" "nginx" {
metadata {
name = "nginx-deployment"
labels = {
app = "nginx"
}
}
spec {
replicas = 1
selector {
match_labels = {
app = "nginx"
}
}
template {
metadata {
labels = {
app = "nginx"
}
}
spec {
container {
name = "nginx"
image = "nginx:latest"
port {
container_port = 80
}
}
}
}
}
}
resource "kubernetes_service" "nginx_lb" {
metadata {
name = "nginx-service"
}
spec {
selector = {
app = "nginx"
}
port {
port = 80
target_port = 80
}
type = "LoadBalancer"
}
}
๐น outputs.tf
Show the public IP after deployment:
output "nginx_load_balancer_ip" {
description = "Public IP of the nginx LoadBalancer service"
value = kubernetes_service.nginx_lb.status[0].load_balancer[0].ingress[0].ip
}
๐ Deploy with Terraform
Run the following commands:
terraform init
terraform apply -auto-approve
Terraform will take a few minutes. Once complete, it will output:
nginx_load_balancer_ip = "<PUBLIC_IP>"
๐ Access NGINX Welcome Page
Open your browser and visit:
http://<PUBLIC_IP>
You should see the NGINX welcome page!
๐งน Cleanup (Optional)
To destroy everything:
terraform destroy -auto-approve
๐ Final Notes
This setup is great for learning and testing.
For production, consider using Helm, Ingress, SSL/TLS, and autoscaling.
๐จ๏ธ Have Questions?
Drop a comment or reach out to me
Subscribe to my newsletter
Read articles from SRINIVAS TIRUNAHARI directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
