Streamlining Azure Infrastructure: A Step-by-Step Guide to Creating a Linux Virtual Machine with Terraform

Streamlining Azure Infrastructure: A Step-by-Step Guide to Creating a Linux Virtual Machine with Terraform

Introduction :-

Embarking on your Azure journey? Wondering how to effortlessly deploy a Linux Virtual Machine (VM) using Terraform? Look no further! In this comprehensive guide, we’ll navigate through the intricate landscape of Azure infrastructure deployment, demystifying the process of creating a Linux VM with the efficiency and flexibility that Terraform offers.

Pre-requestites :-

  • Azure Setup: Azure provides a versatile environment for hosting your virtual machines. Ensure your Azure CLI is installed and authenticated, setting the stage for our Terraform journey.

  • Terraform Installation: Begin by installing Terraform on your local machine or click here to install.

Step-by-Step guide for deploying virtual-machine on Azure --

  1. First create a folder with name linux_vm on your Desktop. Within the folder create terraform configuration files such as main.tf, variable.tf and provider.tf

  2. Copy the below code into your main.tf file.

#main.tf
resource "azurerm_resource_group" "corp_network"{
  name     = var.resource_group_name
  location = var.location
}

resource "azurerm_network_interface" "vm_network" {
  name                = var.network_interface_name
  resource_group_name = azurerm_resource_group.corp_network.name
  location            = azurerm_resource_group.corp_network.location
  ip_configuration {
    name                          = var.ip_configuration_name
    subnet_id                     = var.subnet_id
    private_ip_address_allocation = var.allocation
    public_ip_address_id          = azurerm_public_ip.vm_public_ip.id
  }
    depends_on = [
    var.virtual_network,
    azurerm_public_ip.vm_public_ip ]
  }

resource "azurerm_linux_virtual_machine" "jumpboxvm" {
  name                = var.virtual_machine_name
  resource_group_name = azurerm_resource_group.corp_network.name
  location            = azurerm_resource_group.corp_network.location
  size                = var.size
  admin_username      = var.admin_username
  admin_password      = var.admin_password
  disable_password_authentication = false
  network_interface_ids = [
    azurerm_network_interface.vm_network.id,
  ]

  os_disk {
    caching              = var.caching
    storage_account_type = var.storage_account_type
  }

  source_image_reference {
    publisher = var.publisher
    offer     = var.offer
    sku       = var.sku
    version   = var.version
  }

  depends_on = [
    azurerm_network_interface.vm_network
  ]
}

resource "azurerm_public_ip" "vm_public_ip" {
  name                = var.public_ip_name
  resource_group_name = azurerm_resource_group.corp_network.name
  location            = azurerm_resource_group.corp_network.location
  allocation_method   = var.allocation
  depends_on = [
    azurerm_resource_group.corp_network
  ]
}

3. Paste the below code into your variable.tf file.

variable "resource_group_name" {
  description = "The name of the resource group in which the resources will be created."
  type        = string
  default     = "corp-network"
}

variable "location" {
  description = "(Optional) The location in which the resources will be created."
  type        = string
  default     = "East US 2"
}

variable "network_interface_name" {
  type  = string
  default = "vm-nic"
}

variable "virtual_machine_name" {
  type = string
  default = "jumpboxvm"
}

variable "ip_configuration_name" {
  type = string
  default = "testconfiguration1"
}

variable "subnet_id" {
  type = string
  default = "Your Subnet-id"
}

variable "size" {
  type  = string
  default = "Standard_D2s_V3"
}

variable "admin_username" {
  type  = string
  default = "linuxusr"
}

variable "admin_password" {
  type  = string
  default = "Azure@123"
}

variable "caching" {
  type = string
  default = "ReadWrite"
}
variable "storage_account_type" {
  type = string
  default = "Standard_LRS"
}
variable "publisher" {
  type = string
  default = "Canonical"
}
variable "offer" {
  type = string
  default = "UbuntuServer"
}
variable "sku" {
  type = string
  default = "16.04.0-LTS"
}

variable "version" {
  type = string
  default = "latest"
}

variable "public_ip_name" {
  type = string
 default = "public_ip-vm"
}

variable "allocation" {
  type  = string
  default = "Dynamic"
}

variable "virtual_network" {
  type  = string
  default = "Your Virtual Network Id"
}

3. Now u need to get your subscription-id, client-id, client-secret & tenant-id from your azure portal and paste them into the below provider.tf file

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "3.42.0"
    }
  }
}
provider "azurerm" {
  subscription_id = "YOUR SUBSCRIPTION-ID"
  client_id       = "YOUR CLIENT-ID"
  client_secret   = "YOUR CLIENT-SECRET"
  tenant_id       = "YOUR TENANT-ID"
  features {}
}

4. Next open a terminal or command propmt window and configure your azure credentials. Then locate to the folder in which your terraform files exists.

5. First run the terraform init cmd and then run the terraform plan cmd to view the list of resources that should be created when the terraform configuration is deployed. Last and finally run the terraform apply cmd to deploy your resources on your portal.

Conclusion :-

Congratulations! You’ve successfully navigated the intricate process of creating a Linux Virtual Machine in Azure using Terraform. By embracing Infrastructure as Code (IaC), you’ve not only streamlined your deployment process but also gained a valuable skill set for managing and scaling your Azure infrastructure efficiently.