0
votes

I'm trying to create an Azure spot instance with Terraform. I saw this discussion How do I request azure spot instances using terraform, in a virtual machine scale set?

I still couldn't figure out how provision Azure spot instances with terraform. Then I found this repo https://github.com/terraform-providers/terraform-provider-azurerm/tree/master/examples/vm-scale-set/linux

I used the examples from the repo to come up with my own terraform code. I pushed it to gitlab here's the link..https://gitlab.com/cloud-projectz/azure-spot-terraform.git

Basically what I'm trying to achieve is the creation of a spot instance or spot instances with public ip and ssh access while automating network security rules. When I run terraform apply everything seems to go okay. Then I notice that the public IP is not attached to the spot vm!! I can't figure out how to access the spot vm!! there's also a bash script in the repo that kind of outlines what I attend to accomplish with terraform.

Anybody help me with this? Azure spot vm via terraform with public ip and ssh access?

Thank You

1

1 Answers

0
votes

Regarding the issue, please refer to the following script

provider "azurerm" {

    version = "~>2.0"
        
        features {}

}

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "East Asia"
}

resource "azurerm_virtual_network" "example" {
  name                = "example-network"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  address_space       = ["10.0.0.0/16"]
}
resource "azurerm_subnet" "internal" {
  name                 = "internal"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.2.0/24"]
}
resource "azurerm_network_security_group" "example" {
  name                = "allow-ssh"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  
 security_rule {
    name                       = "SSH"
    priority                   = 1001
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "22"
    source_address_prefix      = "<IP address>"
    destination_address_prefix = "*"
  }
}
resource "azurerm_public_ip_prefix" "example" {
  name                = "example-pip"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_linux_virtual_machine_scale_set" "example" {
  name                            = "example-vmss"
  resource_group_name             = azurerm_resource_group.example.name
  location                        = azurerm_resource_group.example.location
  sku                             = "Standard_F2"
  instances                       = 1
  admin_username                  = "azure"
  eviction_policy                 = "Deallocate"
  priority                        = "Spot"
  max_bid_price                   = 0.5

  admin_ssh_key {
    username   = "azure"
    public_key = file("/root/.ssh/Azure.pub")
  }


  source_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "18.04-LTS"
    version   = "latest"
  }

  network_interface {
    name    = "interface"
    primary = true
    network_security_group_id= azurerm_network_security_group.example.id

    ip_configuration {
      name      = "internal"
      primary   = true
      subnet_id = azurerm_subnet.internal.id

      public_ip_address {
        name                = "first"
        public_ip_prefix_id = azurerm_public_ip_prefix.example.id
    }
  }
}

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

enter image description here enter image description here enter image description here enter image description here