简体   繁体   中英

How do I request azure spot instances using terraform, in a virtual machine scale set?

Apologies in advance if I'm posing the question poorly, but I'd appreciate some help requesting spot instances in the context of a linux vmss.

Here is the reference: https://www.terraform.io/docs/providers/azurerm/r/linux_virtual_machine_scale_set.html#identity

and the relevant piece of it:

在此处输入图像描述

And here is their example json, without any indication of where the optional arguments go:

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      = "adminuser"

  admin_ssh_key {
    username   = "adminuser"
    public_key = file("~/.ssh/id_rsa.pub")
  }

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

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

  network_interface {
    name    = "example"
    primary = true

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

I want to put the following two lines somewhere:

priority            = var.spot_priority
eviction_policy     = var.spot_eviction_policy

but when I put them at the top level, I get this error:

Error: expected priority to be one of [Low Regular], got Spot

(obviously I've got it set to "Spot" in the terraform.tfvars

I've tried inserting them in various blocks, but they give me unexpected argument errors.

Where do I tell terraform that I want spot instances??

EDIT: The problem is solved by passing in "Low" rather than "Spot" in the top level of the vmss resource.

I also use the example that azurerm_linux_virtual_machine_scale_set provide and add the priority and eviction_policy like this:

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      = "adminuser"

  priority = "Spot"
  eviction_policy = "Deallocate"

  admin_ssh_key {
    username   = "adminuser"
    public_key = file("~/.ssh/id_rsa.pub")
  }

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

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

  network_interface {
    name    = "example"
    primary = true

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

And it works fine. When I try to plan it, it shows:

在此处输入图像描述

I use the Terraform version 0.12.19 and the azurerm version 2.20. So you can use the same version as mine and try again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM