【问题标题】:Creating Azure Spot Instances with Terraform使用 Terraform 创建 Azure Spot 实例
【发布时间】:2021-05-04 13:09:52
【问题描述】:
【问题讨论】:
标签:
azure
azure-devops
terraform
virtual-machine
terraform-provider-azure
【解决方案1】:
关于问题,请参考以下脚本
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"
}
}