In the realm of cloud infrastructure management, the ability to define, provision, and manage resources efficiently is paramount. Enter HashiCorp Terraform, an infrastructure as code (IaC) tool designed to streamline this process by allowing you to express your infrastructure requirements in human-readable configuration files. Whether your resources reside in the cloud or on-premises, Terraform empowers you to version, reuse, and share these configurations with ease.
One of Terraform's key strengths lies in its ability to provide a consistent workflow for managing infrastructure throughout its lifecycle. From provisioning to maintenance and eventual decommissioning, Terraform simplifies the process, ensuring reliability and repeatability at every step.
Deploying Azure Kubernetes and Container Registries with Terraform
Let's explore a practical use of Terraform by deploying Azure Kubernetes Service (AKS) and Azure Container Registries (ACR) with Terraform code.
Below is a structured approach to creating your Terraform configuration:
Project Structure
azure-k8s-terraform/
├── main.tf
├── variables.tf
├── outputs.tf
├── providers.tf
└── terraform.tfvars
Provider Configuration (providers.tf)
To start your Terraform configuration for Azure, you'll first define the provider settings. This ensures Terraform knows how to interact with Azure resources.
provider "azurerm" {
features {}
}
Variable Definitions (variables.tf)
Next, define the variables needed for your deployment. These variables provide flexibility and customization while keeping your configuration clean and manageable.
variable "resource_group_name" {
description = "The name of the resource group"
type = string
}
variable "location" {
description = "The Azure region where resources will be created"
type = string
}
variable "aks_cluster_name" {
description = "The name of the AKS cluster"
type = string
}
variable "acr_name" {
description = "The name of the Azure Container Registry"
type = string
}
variable "kubernetes_version" {
description = "The version of Kubernetes for the AKS cluster"
type = string
default = "1.28.9" # Adjust this to the desired Kubernetes version
}
variable "node_count" {
description = "The number of nodes in the AKS cluster"
type = number
default = 2
}
variable "node_vm_size" {
description = "The size of the Virtual Machines in the AKS cluster"
type = string
default = "Standard_DS2_v2"
}
Terraform Configuration (terraform.tfvars)
In this file, you'll specify the values for your variables, customizing them to fit your specific deployment needs.
resource_group_name = "devops-rg"
location = "westus2"
aks_cluster_name = "devops-aks"
acr_name = "myACR"
kubernetes_version = "1.28.9"
node_count = 2
node_vm_size = "Standard_DS2_v2"
Resource Definitions (main.tf)
Now comes the exciting part – defining the resources you want to provision. Below is an example of how you can create an AKS cluster and an ACR.
resource "azurerm_resource_group" "rg" {
name = var.resource_group_name
location = var.location
}
# Azure Kubernetes Cluster
resource "azurerm_kubernetes_cluster" "k8s" {
name = var.aks_cluster_name
kubernetes_version = var.kubernetes_version
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
dns_prefix = "${var.aks_cluster_name}-dns"
default_node_pool {
name = "agentpool"
vm_size = var.node_vm_size
node_count = var.node_count
}
identity {
type = "SystemAssigned"
}
# For production change to "Standard"
sku_tier = "Free"
network_profile {
network_plugin = "azure"
}
}
resource "azurerm_container_registry" "main" {
name = var.acr_name
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
sku = "Standard"
admin_enabled = true
}
Deployment Workflow
Once your Terraform configuration is ready, follow these steps to deploy your infrastructure:
Plan: Generate an execution plan to preview the changes Terraform will make.
terraform plan -out main.tfplan
Apply: Execute the planned changes to create the infrastructure.
terraform apply "main.tfplan"
Destroy: When you no longer need the resources, use Terraform to tear down the infrastructure.
terraform destroy
With this Terraform configuration in place, deploying and managing Azure infrastructure becomes easy. Embrace the power of infrastructure as code to achieve greater efficiency and scalability in your cloud deployments.