Deploy Azure Spot Instance With Terraform

Azure Spot Instances are a type of virtual machine instance that allows users to take advantage of unused Azure compute capacity at a significantly reduced cost.

Spot Instances are ideal for workloads that are flexible in terms of timing, meaning they can be run during periods of low demand when capacity is available. This makes Spot Instances perfect for running batch processing jobs, testing and development environments, and other workloads that are not time-critical.

How Azure Spot Instances Work

Azure Spot Instances work on a bidding system. Users place bids for the capacity they need, and if their bid is higher than the current market price, they can access the available capacity. When the Spot price exceeds the user’s bid, the instance is automatically evicted, and the user’s workloads are paused. This means that Spot Instances are not suitable for workloads that require constant availability or have strict SLAs.

Terraform

The end-to-end code to deploy a Windows Server 2022 VM is below however the parts \ components that make a VM a spot VM are:

  eviction_policy     = "Deallocate"
  priority            =  "Spot"

Note: You can change the eviction_policy to delete.

Full Code

To deploy a spot instance VM using Terraform use the code blow.

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

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "rg" {
  name     = "SpotVM"
  location = "southeastasia"
}


resource "azurerm_virtual_network" "rg" {
  name                = "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                = "spotvm"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  size                = "Standard_B4ms"
  admin_username      = "adminuser"
  admin_password      = "setpasswordhere"
  eviction_policy     = "Deallocate"
  priority            =  "Spot"
  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       = "2022-datacenter-azure-edition"
    version   = "latest"
  }
 
 }

Benefits of Azure Spot Instances

The primary benefit of using Azure Spot Instances is cost savings. Users can access Azure compute capacity at a significantly reduced cost, up to 90% less than the standard pay-as-you-go pricing. This makes Spot Instances ideal for running workloads that are not time-critical or require high availability. Spot Instances can also be used in conjunction with Azure Reserved Instances and Azure Hybrid Benefit, further reducing costs.

Processing…
Success! You're on the list.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.