Automation Tutorial: Provision an AKS Cluster (Azure) with Terraform

Automation Tutorial: Provision an AKS Cluster (Azure) with Terraform

This tutorial will show you on how to create a Azure Kubernetes Cluster (AKS) by using the Terraform tool.


  1. Azure Account(
  2. Install Azure CLI (
  3. Azure Login ( az login)
  4. Terraform

Set up and initialize your Terraform Folder Workspace

You Folder structure become like below; provisions a resource group and an AKS cluster. The default_node_pool defines the number of VMs and the VM type the cluster uses. The addon_profile enables the Kubernetes dashboard. declares the appID and password so Terraform can use reference its configuration:

terraform.tfvars defines the appId and password variables to authenticate to Azure declares values that can be useful to interact with your AKS cluster sets the Terraform version to at least 0.13 and defines the required_provider block

Create an Active Directory service principal account

We need to configure the provider with the subscription_id, client_id, client_secret and tenant_id. Set the following environment variables with the values which correspond to your account.

export ARM_SUBSCRIPTION_ID=xxxxxxxx
export ARM_CLIENT_ID=xxxxxxx
export ARM_CLIENT_SECRET=xxxxxxx
export ARM_TENANT_ID=xxxxxxxx

We can create a new Service Account by execute command below;


Initialize Terraform

After you have saved your customized variables file, initialize your Terraform workspace, which will download the provider and initialize it with the values provided in your terraform.tfvars file.


Run / Execute Terraform and Provision the AKS cluster

It will execute below;

This process should take approximately 10 minutes. Upon successful application, your terminal prints the outputs defined in


Configure kubectl

Create and Access Kubernetes Dashboard

To verify that your cluster is configured correctly and running, you will navigate to it in your local browser. We need to create a ClusterRoleBinding to use the Kubernetes dashboard. This gives the cluster-admin permission to access the kubernetes-dashboard. you don't need to configure your Terraform Kubernetes Provider since you create this using Terraform, kubectl .


Finally, to access the Kubernetes dashboard, run the following command, customized with your cluster name instead of sample-aks-. This will continue running until you stop the process by pressing CTRL + C.


You should be able to access the Kubernetes dashboard at


If want to destroy any resources you create once you are done with this tutorial. Run the destroy command and confirm with yes in your terminal.

Recap / Summary

We had learned how to use Terraform to provision Azure Kubernetest Cluster by following steps;

  1. Set up and initialize your Terraform workspace
  2. Create an Active Directory service principal account
  3. Initialize Terraform
  4. Run / Execute Terraform and Provision the AKS cluster
  5. Configure kubectl locally
  6. Access Kubernetes Dashboard
  7. Clean up your tutorial workspace

Share Tweet Send
You've successfully subscribed to DevOps4Me
Great! Next, complete checkout for full access to DevOps4Me
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.