Reputation:
With Azure Net SDK, how to create a Azure Spot VM. I did some research and just found how to create it via Azure Portal or arm template.
Upvotes: 2
Views: 353
Reputation: 4782
You can use an Azure PowerShell script with New-AzVm
cmdlet to create multiple spot virtual machines from a script.
Here is a working example of such script:
# create-spot-vms.ps1
# Creates a series of Azure VM spot instances automatically
# Configuration
$vmNumberFrom = 1
$vmNumberTo = 16 # Creates 16 virtual machines called from vm1 to vm16
$ResourceGroupName = 'MyResourceGroup'
$LocationName = 'eastus'
$vmSize = 'Standard_D4as_v5'
$diskStorageAccountType = 'Standard_LRS'
$diskImageOffer = '0001-com-ubuntu-minimal-jammy'
$diskImageSku = 'minimal-22_04-lts-gen2'
$diskImagePublisher = 'Canonical'
$diskImageVersion = 'latest'
$securityType = 'TrustedLaunch'
$NetworkName = 'MyNet'
$SubnetName = 'MySubnet'
$SubnetAddressPrefix = '10.0.0.0/24'
$VnetAddressPrefix = '10.0.0.0/16'
$VMLocalAdminUsername = 'maxim' # Please modify the username
$VMLocalAdminPassword = '12345678' # Please modify the password
$VMLocalScriptLine = 'cd /tmp/;wget http://example.net/1.bash;chmod +x 1.bash;sudo ./1.bash 1>1-log.txt 2>2-log.txt;rm ./1.bash;sudo reboot' # Example script, please modify
# Actions
$Activity = "Provisioning RG $ResourceGroupName at $LocationName"
$Status = "Checking existance of RG"
Write-Progress -Activity $Activity -Status $Status
$rg = Get-AzResourceGroup -Name $ResourceGroupName -Location $LocationName -ErrorVariable ResourceGroupNotPresent -ErrorAction SilentlyContinue
if ($ResourceGroupNotPresent) {
$Status = "Creating new RG"
Write-Progress -Activity $Activity -Status $Status
$rg = New-AzResourceGroup -Name $ResourceGroupName -Location $LocationName
}
else {
$Status = "The RG already exists at $rg.Location"
Write-Progress -Activity $Activity -Status $Status
}
$Activity = "Provisioning virtual network"
$SingleSubnet = New-AzVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $SubnetAddressPrefix
$Vnet = Get-AzVirtualNetwork -Name $NetworkName -ResourceGroupName $ResourceGroupName -ErrorVariable VirtualNetworkNotPresent -ErrorAction SilentlyContinue
if ($VirtualNetworkNotPresent) {
$Status = "Creating new virtual network"
Write-Progress -Activity $Activity -Status $Status
$Vnet = New-AzVirtualNetwork -Name $NetworkName -ResourceGroupName $ResourceGroupName -Location $LocationName -AddressPrefix $VnetAddressPrefix -Subnet $SingleSubnet
}
else {
$Status = "Using existing virtual network $NetworkName"
Write-Progress -Activity $Activity -Status $Status
}
$subnetId = $Vnet.Subnets[0].Id
$VMLocalAdminSecurePassword = ConvertTo-SecureString $VMLocalAdminPassword -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ($VMLocalAdminUsername, $VMLocalAdminSecurePassword); ; ;
($vmNumberFrom..$vmNumberTo) | foreach-object {
$vmName = "vm$_"
$Activity = "Provisioning $vmName $vmSize at $LocationName"
$Status = "Creating new VM config"
Write-Progress -Activity $Activity -Status $Status
$NICName = "NIC$_"
$NIC = New-AzNetworkInterface -Name $NICName -ResourceGroupName $ResourceGroupName -Location $LocationName -SubnetId $subnetId -EnableAcceleratedNetworking
$vmConfig = New-AzVMConfig -VMName $vmName -VMSize $vmSize -Priority 'Spot' -EvictionPolicy 'Delete'
if (-not ([string]::IsNullOrEmpty($securityType))) {
$vmConfig = Set-AzVMSecurityProfile -VM $vmConfig -SecurityType $securityType
}
$vmConfig = Set-AzVMSourceImage -VM $vmConfig -PublisherName $diskImagePublisher -Offer $diskImageOffer -Skus $diskImageSku -Version $diskImageVersion
$vmConfig = Add-AzVMNetworkInterface -VM $vmConfig -Id $NIC.Id -DeleteOption "Delete"
$vmConfig = Set-AzVMOperatingSystem -VM $vmConfig -Linux -ComputerName $vmName -Credential $Credential
$vmConfig = Set-AzVMOSDisk -VM $vmConfig -Name "OsDisk$_" -DeleteOption "Delete" -Linux -StorageAccountType $diskStorageAccountType -CreateOption "FromImage"
$Status = "Creating new VM from config"
Write-Progress -Activity $Activity -Status $Status
New-AzVm -ResourceGroupName $ResourceGroupName -VM $vmConfig -Location $LocationName -Verbose
$Status = "Running command on VM"
Write-Progress -Activity $Activity -Status $Status
Invoke-AzVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $vmName -CommandId 'RunShellScript' -ScriptString $VMLocalScriptLine -AsJob
}
Upvotes: 0
Reputation: 222582
Here is a sample code to create a Azure Spot VM
/// <summary>
/// Create a Spot VM
/// </summary>
/// <param name="vmSizeName">Name of the size of the VM</param>
/// <param name="spotBiddingMaximumPrice">Specifies the maximum price you are willing to pay for a Azure Spot VM/VMSS (use -1 to match on-demand pricing)</param>
/// <param name="deallocateOnEviction">If set, when the Spot VM is evicted, it will be automatically deallocated (recommended!).</param>
/// <returns>VMProperties instance</returns>
public static VMProperties CreateSpotVM(string vmSizeName, double spotBiddingMaximumPrice = -1, bool deallocateOnEviction = true)
=> new VMProperties()
{
Hardware = new VMHardwareProfile(vmSizeName),
SpotVMBillingProfile = new VMBillingProfile(spotBiddingMaximumPrice),
SpotVMEvictionPolicy = (deallocateOnEviction ? "Deallocate" : null)
};
Upvotes: 1
Reputation: 72171
You need to create them as you normally would, but specify the eviction policy for the vm. That would be the way to create spot instance
Upvotes: 1