Deploying an Azure Windows VM using Terraform IaC

Introduction

This article will focus on deploying an Azure Windows virtual machine using Terraform as infrastructure as code.

In this article, you will learn how to set up your Azure account, configure your Terraform environment, write Terraform code to define your VM and associated resources, and deploy your VM to Azure using Terraform.

If you're interested, my GitHub repository is open for use. You can access it at https://github.com/shanuka-thushara/Azure-Windows-VM-Using-Terraform-IaC

Prerequisites

  • Azure subscription
  • Install and configure Terraform
  • Visual Studio Code

Solution Diagram

Step 1. Create a file directory in your local location.

Step 2. Create a named Provider.tf file.

Step 3. Insert the following code in Provide.tf file.

provider "azurerm" {
  features {}
}

Step 4. In Main.tf file use the following code to create a resource group (RG).

resource "azurerm_resource_group" "RG" {
  name     = "Shanuka-RG"
  location = "East US"
}

Step 5. In Main.tf file use the following code to create a Virtual Network and Subnet.

resource "azurerm_virtual_network" "VNet" {
  name                = "Shanuka-VNet"
  address_space       = ["10.99.0.0/16"]
  location            = azurerm_resource_group.RG.location
  resource_group_name = azurerm_resource_group.RG.name
}

resource "azurerm_subnet" "AzureSubnet" {
  name                 = "ShanukaSubnet"
  resource_group_name  = azurerm_resource_group.RG.name
  virtual_network_name = azurerm_virtual_network.VNet.name
  address_prefixes     = ["10.99.2.0/24"]
}

Note: As per your requirement you can choose the IP address space.

Step 6. Create a Network Interface Card (nic) using the following code.

resource "azurerm_network_interface" "nic" {
  name                = "shanuka-nic"
  location            = azurerm_resource_group.RG.location
  resource_group_name = azurerm_resource_group.RG.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.ShanukaSubnet.id
    private_ip_address_allocation = "Dynamic"
  }
}

Step 7. Create a Public IP address for VM access.

resource "azurerm_public_ip" "PubIP" {
  name                = "Shanuka-PIP"
  resource_group_name = azurerm_resource_group.RG.name
  location            = azurerm_resource_group.RG.location
  allocation_method   = "Static"
  }

Step 8. Create a Network Security Group (NSG) for securing your VM.

resource "azurerm_network_security_group" "NSG" {
  name                = "Shanuka-NSG"
  location            = azurerm_resource_group.RG.location
  resource_group_name = azurerm_resource_group.RG.name
}

Step 9. Create a Windows Virtual Machine.

 

resource "azurerm_windows_virtual_machine" "VM" {
  name                = "Shanuka-VM"
  resource_group_name = azurerm_resource_group.RG.name
  location            = azurerm_resource_group.RG.location
  size                = "Standard_B2ms"
  admin_username      = "azureuser"
  admin_password      = "P@$$w0rd1234!"
  network_interface_ids = [
    azurerm_network_interface.nic.id,
  ]

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

  source_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2022-Datacenter"
    version   = "latest"
  }
}

Note: Before running this configuration file, make sure you have the Azure CLI installed and have authenticated using the az login command. You'll also need to replace the placeholders for subscription_id, client_id, client_secret, and tenant_id with your own values.

Once you have done the az login command automatically you will get the Microsoft Azure sign-in page as shown in the below snapshot.

Key commands in Terraform 

  • Terraform init - Initialize a terraform working directory. Example: terraform init
  • Terraform plan - Generate and show an execution plan. Example: terraform plan
  • terraform apply - Build or change infrastructure. Example: terraform apply 

Step 1. In Terminal type terraform plan command

Step 2. Open the terminal and type terraform apply -auto-approve command.

It successfully created the Azure infrastructure.

Step 3. Once deployment is done, you can verify your resource from the Azure portal.

Conclusion

This article taught us how to deploy Azure virtual machines using Terraform code Infrastructure as Code (IaC). If you have any questions, don't hesitate to comment below.


Similar Articles