โ˜๏ธ CloudSchool.ai โ€” Infrastructure as Code

๐ŸŸฃ Terraform (HCL) โ€” Ubuntu VM Deployment

Terraform by HashiCorp is an open-source IaC tool using HashiCorp Configuration Language (HCL). It supports multi-cloud deployments and maintains a state file to track real infrastructure.

TerraformHCLAzure ProviderUbuntu 22.04 LTSState Management

Terraform Configuration โ€” main.tf

HCLCloudSchool.ai
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.90"
    }
  }
}

provider "azurerm" {
  features {}
}

variable "admin_password" {
  type      = string
  sensitive = true
}

resource "azurerm_resource_group" "rg" {
  name     = "rg-cloudschool-iac"
  location = "East US"
}

resource "azurerm_virtual_network" "vnet" {
  name                = "cloudschool-vnet"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
}

resource "azurerm_subnet" "subnet" {
  name                 = "default"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefixes     = ["10.0.0.0/24"]
}

resource "azurerm_network_security_group" "nsg" {
  name                = "cloudschool-nsg"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  security_rule {
    name                       = "Allow-SSH"
    priority                   = 1000
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "22"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
  }
}

resource "azurerm_public_ip" "pip" {
  name                = "cloudschool-pip"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  allocation_method   = "Dynamic"
}

resource "azurerm_network_interface" "nic" {
  name                = "cloudschool-nic"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  ip_configuration {
    name                          = "ipconfig1"
    subnet_id                     = azurerm_subnet.subnet.id
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = azurerm_public_ip.pip.id
  }
}

resource "azurerm_network_interface_security_group_association" "nsg_assoc" {
  network_interface_id      = azurerm_network_interface.nic.id
  network_security_group_id = azurerm_network_security_group.nsg.id
}

resource "azurerm_linux_virtual_machine" "vm" {
  name                            = "cloudschool-ubuntu-vm"
  resource_group_name             = azurerm_resource_group.rg.name
  location                        = azurerm_resource_group.rg.location
  size                            = "Standard_B1s"
  admin_username                  = "cloudadmin"
  admin_password                  = var.admin_password
  disable_password_authentication = false
  network_interface_ids           = [azurerm_network_interface.nic.id]

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    publisher = "Canonical"
    offer     = "0001-com-ubuntu-server-jammy"
    sku       = "22_04-lts-gen2"
    version   = "latest"
  }
}

output "public_ip" {
  value = azurerm_public_ip.pip.ip_address
}

output "ssh_command" {
  value = "ssh cloudadmin@${azurerm_public_ip.pip.ip_address}"
}

Deployment Commands

BASHCloudSchool.ai
# Authenticate to Azure
az login

# Initialize Terraform (downloads Azure provider)
terraform init

# Preview the deployment plan
terraform plan -var="admin_password=YourSecureP@ssw0rd!"

# Apply the configuration
terraform apply -var="admin_password=YourSecureP@ssw0rd!" -auto-approve

# Connect via SSH
ssh cloudadmin@$(terraform output -raw public_ip)

# Destroy all resources when done
terraform destroy -var="admin_password=YourSecureP@ssw0rd!" -auto-approve

Key Concepts

๐Ÿ“š Continue learning: CloudSchool.ai Knowledge Base ยทIaC Glossary