Deploy a Virtual Machine To Azure Using Terraform

In this blog post, I will show you how to deploy a virtual machine to Microsoft Azure using Terraform.

In the previous article, I showed you how to set up your macOS or Windows machine with Terraform and load all the tools.

# Configure the Azure provider
terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = ">= 2.26"
    }
  }
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "rg" {
  name     = "TFRDemo"
  location = "australiasoutheast"
}
resource "azurerm_virtual_network" "rg" {
  name                = "rg-network"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
}

resource "azurerm_subnet" "rg" {
  name                 = "internal"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.rg.name
  address_prefixes     = ["10.0.2.0/24"]
}

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

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

resource "azurerm_windows_virtual_machine" "rg" {
  name                = "rg-machine"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  size                = "Standard_F2"
  admin_username      = "adminuser"
  admin_password      = "Typeyourpasswordhere!"
  network_interface_ids = [
    azurerm_network_interface.rg.id,
  ]

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

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

Code

To use Terraform for Azure deployment (or any other public cloud) we use .TF files that that contain all the needed configuration. Below I have a code that deploy a Windows Virtual Machine to Microsoft Azure. The code creates all the components (RG, Storage, NICs, etc).

Note: The code also set the admin username and password for the VM, make sure you change it after or before.

Deploy

To start the deployment, save the code to a file called VM.tf and place it in a folder. This is important because if you have multiple.TF files Terraform will run all of them.

Once you save the file, access it from the command-line and run the following line to initiate Terraform.

terraform init

Next, we need to check if the code is OK and do a dry run that will validate everything and let us know if we can move to deploying the code.

Terraform plan

Note: the when you see the green plus signs it means that terraform will create \ add resources. If you see red it means that something will get removed. Grey means we will update something.

To deploy the VM we will use the apply command as shown below.

terraform apply

The process will take a few minutes.

Once done, I can see the VM in the portal.

I an also use the show command to check the deployment and what was deployed.

 terraform show

To delete everything we use the destroy command that will delete all the resources the Terraform has created. I think this is the best part of Terraform which allows us to clean all the resources from Azure.

 terraform destroy