Deploying an Azure Windows VM using Terraform IaC


31932
7k shares, 31932 points

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.

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     = "Test-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                = "Test-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                 = "TestSubnet"
  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                = "test-nic"
  location            = azurerm_resource_group.RG.location
  resource_group_name = azurerm_resource_group.RG.name

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

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

resource "azurerm_public_ip" "PubIP" {
  name                = "Test-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                = "Test-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.

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.

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.


Like it? Share with your friends!

31932
7k shares, 31932 points

What's Your Reaction?

hate hate
5000
hate
confused confused
15000
confused
fail fail
10000
fail
fun fun
8333
fun
geeky geeky
6666
geeky
love love
1666
love
lol lol
3333
lol
omg omg
15000
omg
win win
10000
win
Tasadduq

0 Comments

Your email address will not be published. Required fields are marked *

Choose A Format
Personality quiz
Series of questions that intends to reveal something about the personality
Trivia quiz
Series of questions with right and wrong answers that intends to check knowledge
Poll
Voting to make decisions or determine opinions
Story
Formatted Text with Embeds and Visuals
List
The Classic Internet Listicles
Countdown
The Classic Internet Countdowns
Open List
Submit your own item and vote up for the best submission
Ranked List
Upvote or downvote to decide the best list item
Meme
Upload your own images to make custom memes
Video
Youtube and Vimeo Embeds
Audio
Soundcloud or Mixcloud Embeds
Image
Photo or GIF
Gif
GIF format