Terminal prompt logo for Ali Haidry — alihaidry.dev~/AHAli Haidryalihaidry.dev
Phase 2 of 6Published

Infrastructure with Terraform

Everything below the cluster is code: AKS, the registry, Key Vault, DNS and the identities that let GitHub and pods talk to Azure without a single stored secret.

TerraformAKSACRKey VaultAzureUpdated Nov 2026

Goal

What this phase delivers

A repeatable Azure platform that you can destroy and rebuild: three node pools, a private registry, a vault with RBAC, workload identity for pods, GitHub OIDC for the pipeline and DNS for the domain, all in Terraform with remote state in a separate resource group.

  • Remote stateA separate resource group and private storage account, so destroying the platform never touches the state.Done
  • AKS with three node poolssystem (tainted for add-ons), apps for workloads, monitoring (tainted) for observability.Done
  • Registry and vaultACR Basic with admin disabled; Key Vault with RBAC and soft delete.Done
  • Identity without secretsWorkload identity for pods and GitHub OIDC for the pipeline. No client secret exists.Done
  • DNSAn Azure DNS zone for the domain with Terraform-managed records.Done

Steps and commands

Run these in dev first. Every step has a check that proves it worked, so you are never guessing. The first apply planned and created 29 resources, and a second plan showed no changes.

  1. Check the tools and sign in with MFA

    Confirm versions and log in. If your tenant requires MFA for management operations, use the claims login command the error prints.

    $ az version
    $ terraform version      # needs >= 1.6.0
    $ az login
    $ az account show -o table
    Check: the subscription you expect is the active one.
  2. Create the remote state

    A separate resource group, a private storage account and a container, using your Azure AD token instead of an account key. In Git Bash, stop path rewriting first.

    $ export MSYS_NO_PATHCONV=1
    $ az group create rg-paktech-tfstate
    $ az storage container create --auth-mode login ...
    Check: terraform init succeeds and connects to the azurerm backend.
  3. Validate, plan, read the plan, apply

    Read the plan before you apply it. Save it to a file so apply runs exactly what you reviewed.

    $ terraform init
    $ terraform fmt -recursive
    $ terraform validate
    $ terraform plan -out tfplan
    $ terraform apply tfplan
    Check: the plan says 29 to add, 0 to change, 0 to destroy, and a second plan shows no changes.
  4. Verify the platform

    Check the node pools and taints, the registry settings and the vault, then read the outputs.

    $ kubectl get nodes -L workload,agentpool
    $ az acr show -n paktechacr --query "{sku:sku.name,admin:adminUserEnabled}" -o table
    $ terraform output
    Check: three pools are present, ACR admin is false, and the DNS name servers are printed.
  5. Stop the cluster when you are not working

    The lab cluster costs money while it runs, so stop it between sessions.

    $ az aks stop  -g rg-paktech-prod -n aks-paktech-prod
    $ az aks start -g rg-paktech-prod -n aks-paktech-prod
    Check: powerState.code reads Stopped, then Running after a start.

Workflow

Infrastructure changes follow the same read-then-apply path every time.

  1. STEP 1EditTerraform files
  2. STEP 2Validatefmt and validate
  3. STEP 3PlanSave to a file
  4. STEP 4ReviewRead every change
  5. STEP 5ApplyApply the saved plan

Rollback: Revert the change in Git and apply again, or destroy only the resources you added. The state lives outside the platform, so it survives a destroy.

Diagram

Resource map: what Terraform creates and how the pieces connect
Figure: what Terraform creates and how the pieces connect.

Issues met

These are real, from the run. The surprises are the useful part.

The bootstrap script stopped with AADSTS50076
Cause
The tenant requires MFA for management operations, and the old login token had no MFA claim.
Fix
Ran the claims login command the error printed, completed MFA and re-ran the script.
terraform init failed with 403 listing blobs
Cause
Git Bash rewrote the /subscriptions/... scope into a Windows path, so a role assignment silently failed, and the script hid the error with 2>/dev/null.
Fix
MSYS_NO_PATHCONV=1, create the assignment by hand, and stop hiding errors in the script.

Trade-offs

Where PostgreSQL and Redis run
I chose: In the cluster, as StatefulSets

Why: It gives hands-on experience with storage, identity and backups.

It costs: You own upgrades and recovery. In production I would use managed services.

Registry tier
I chose: ACR Basic with admin disabled

Why: It is cheap for a lab, and access goes through identities instead of a shared password.

It costs: No geo-replication or private link on this tier.

Interview questions

Why keep Terraform state in a separate resource group?
So destroying or rebuilding the platform cannot delete the state that describes it, and access to state can be controlled separately.
How does the pipeline authenticate to Azure without a secret?
GitHub issues a short-lived OIDC token for the workflow, and Azure trusts it through a federated credential scoped to the repository and branch or environment.
Why three node pools?
Isolation. Tainted system and monitoring pools keep add-ons and observability load from competing with the chat workloads.