In scenarios such as training, demos and trials, you may want to create virtual machines and delete them automatically after a fixed duration so that you don’t incur unnecessary costs. We recently announced a feature which allows you to do just that - set an expiration date for a lab VM.
You can create a lab VM with an expiration date using an ARM template by specifying the expirationDate property for the VM. You can check out a sample Resource Manager template in our public GitHub repository. You can also modify any of the existing sample Resource Manager templates for the VM creation (name starting with 101-dtl-create-vm) by adding the expirationDate property as follows.
You can set an expiration date for an already existing lab VM or all the VMs in the lab using Azure PowerShell cmdlets. Sharing a sample script that you can use.
# Values to change
$subscriptionId = '<Enter the subscription Id that contains lab>'
$labResourceGroup = '<Enter the lab resource group>'
$labName = '<Enter the lab name>'
$VmName = '<Enter the VmName>'
$expirationDate = '<Enter the expiration date e.g. 2016-09-16>'
# Log into your Azure account
Select-AzureRmSubscription -SubscriptionId $subscriptionId
$VmResourceId = "subscriptions/$subscriptionId/resourcegroups/$labResourceGroup/providers/microsoft.devtestlab/labs/$labName/virtualmachines/$VmName"
$vm = Get-AzureRmResource -ResourceId $VmResourceId -ExpandProperties
# Get all the Vm properties
$VmProperties = $vm.Properties
# Set the expirationDate property
$VmProperties | Add-Member -MemberType NoteProperty -Name expirationDate -Value $expirationDate
Set-AzureRmResource -ResourceId $VmResourceId -Properties $properties -Force
Please try the feature and let us know how we can make it better by sharing your ideas and suggestions at the DevTest Labs feedback forum.
If you run into any problems with the feature or have any questions, we are always ready to help you at our MSDN forum.