Månadsinformation från Arkitektforum informerar om uppdateringar, ändringar, nya produkter och matnyttig information rörande Azure Dev, Azure Ops, AWS, .Net och Biztalk).
How to schedule import and dispose of Virtual Machines in Windows Azure
Due to the price model of Windows Azure you are forced to dispose a virtual machine to prevent further costs of machines that are no longer in use. If you are familiar with the price model of Windows Azure you would know that you are charged per hour the virtual machines are deployed to Azure. At first sight it seems to be enough just to stop the virtual machine to prevent further charges, however this is not the case. As I mentioned before you have to dispose the virtual machine to get around this. As a developer it is contrary to my principles to delete a virtual machine that I have spent so much time to develop. Even though I may seem wrong, this is what you have to do to avoid getting charged. The main purpose with this article is to show how you could automate this process and even schedule when the virtual machine should be deployed or disposed in purpose to keep the costs down. Due to lack of an out of box solution for this scenario, I would like to share my way of solving this.
I am by far no expert on the subject and I am aware that there are several different ways of solving this, however this is the solution I have found to the problem.
Create a Virtual Machine
In the following example I am going to use one Virtual machine to show you how to first delete and then restore/ import you virtual machine. Begin by creating a new virtual machine in the Azure portal, choose an image from gallery and press next.
Specify the preferred machine settings and choose size of virtual machine and press next, the bigger the more expensive, so do not choose a bigger Virtual machine than you require.
Specify the preferred machine modes and press next.
Specify if you want to use availability set and press next to finish creating you Virtual Machine.
Configure Windows Azure PowerShell
In general you can keep your costs down just by deleting and recreating the Virtual Machine by hand in the Azure portal. This works as long as you do not delete the VHD. However you might find this a bit time consuming and find that you want to automate the whole process or even schedule it. If this is the case you can accomplish this by using Windows Azure PowerShell. To begin you have to download the latest cmdlets for windows azure, you can find them here: https://www.windowsazure.com/en-us/downloads/?fb=sv-se.
Install the Azure cmdlets you downloaded from the link above by following the steps in the installation wizard. After you have completed this step and you have ensured that the azure cmdlets where properly installed, you can move on to the next step.
The next step will be to download and save your .publishsettings file, do this by open the following link in Windows Explorer: https://windows.azure.com/download/publishprofile.aspx
The .publishsettings file contains a list of all subscriptions for which your Microsoft Account is an admin or co-admin, as well as a base64 encoded management certificate. With the .publishsettings file you can execute the “Import-AzurePublishSettingsFile”-command to establish and configure connectivity between your machine, Windows Azure PowerShell cmdlets and Windows Azure. This same file can also be imported to Visual Studio to establish connectivity between Visual Studio and Windows Azure.
Import publishsettings file
To import the .publishsettingsfile you have to run the following command in Windows Azure PowerShell:
If you prefer to manually configure your subscription you can do this by using “Set-AzureSubscription”, I won’t cover this in the current article. Whether you choose to do this manually or follow my example, the subscription settings are persisted in the following location:
C:\Users\user\AppData\Roaming\Windows Azure Powershell
This means you do not have to perform this step every time you want to use PowerShell to handle your Azure subscription.
Configure Storage Account
When dealing with Virtual Machines in Windows Azure you have to specify what storage account that the cmdlets should use when they need to access storage. Run the following command to determine whether you have a storage account configured or not.
If no storage account is returned by the command, you will have to create one.
When you have created a storage account, you have to set this to be the default storage account for your subscription.
Alright! After finishing the above listed steps, we are finally ready to start creating scripts to affect the virtual machines in Azure. The first step would be to export the Virtual machine in purpose to be able to restore it later.
Export Virtual Machine
Run following command in Windows Azure PowerShell to export the config file for your Virtual Machine:
Export-AzureVM -ServiceName ‘<YourServiceName>’ -Name ‘<YourVitualMachineName>’ -Path ‘<PreferredStoragePath>’
Dispose Virtual Machine
The second step would be to dispose the virtual machine by creating a batch file that invokes a PowerShell script. By creating a batch file to handle the disposal of the Virtual Machine we are making it very easy to schedule this process later in the example. Create a new text file and save it with the following name and extension “RemoveVM.bat”. Right click the .bat-file and choose to edit the file. Type the following line in the document and press save.
@powershell -command “Remove-AzureVM -ServiceName ‘<YourServiceName>’ -Name ‘<YourVitualMachineName>'”
After you have saved the file, run the file. The process should look something like the picture below. After the script have finished, the virtual machine should have been removed from Azure, double check in the portal.
Remove Cloud Service
After about 10 minutes after you have removed your Virtual Machine, a Cloud Service should appear in the Azure-portal. A Cloud Service is always created in purpose to store your virtual machines. If you just create one Virtual Machine, the Cloud Service never appears in the portal until you remove the Virtual Machine. This phenomenon seems based on the following: when creating just one Virtual Machine you are not interested in which Cloud Service your machine is located in, however if you create more than one Virtual Machine and decide to connect two or more machines with each other you have to know which Cloud Service the machine you want to connect with is located in. When connecting two machines, basically what happens is that the machines are placed in the same Cloud Service. So why would not the Cloud Service appear in the portal when just creating one Virtual Machine? It seems that the Windows Azure team has decided that you are not going to be interested in the Cloud Service unless you create more than one machine and therefore it is not shown in the portal until you have created at least two Virtual Machines.
As mentioned above the Cloud Service is not as important for one Virtual Machine as it would be if you have several machines. Therefore I prefer to remove the Cloud Service that was created with the Virtual Machine and later recreate it when restoring the Virtual Machine. To do this we create a .bat-file like the one we created to remove the Virtual Machine, this time the content of the script should look like this:
@powershell -command “Remove-AzureService ‘<YourServiceName>’ -Force -confirm:$false”
The last bit in the script “-Force -confirm:$false” has the task of confirming the decision dialog that appears when trying to delete a Cloud Service. Save the file with the following name and extension “RemoveCloudService.bat” and run it, but first remember to check in the portal that the Cloud Service has appeared after you removed the Virtual Machine, as I mentioned before it takes about 10 minutes to do so. Double check that the Cloud Service no longer exists in the portal after you have run the script and move on to the next step.
Restore the Virtual Machine
Now it is time for restoring the Virtual Machine we deleted a couple of minutes ago. To do this you will have to create an additional batch-file named “ImortVirtualMachine.bat” with following script:
@powershell -command “Import-AzureVM -Path ‘<SamePathAsYouChoosedWhenExporting>’ | New-AzureVM -ServiceName ‘<YourServiceName>’ -Location ‘North Europe'”
After saving the file, run it and it should result in something like the picture below. Make sure to double check that the machine actually pops up in the portal, if it does, try to connect to it to verify that everything went according to plan.
Schedule import and disposal of Virtual Machine
Let’s say that you want your virtual machine to be restored and imported to Windows Azure every morning 8am and deleted 11am to maintain low costs, then Task Scheduler is one way solve this! To make this happen you have put up and configure three different scheduled tasks. We begin by opening Task Scheduler and create a new task.
The next step is to add a new Trigger and configure it to determine when the script should be fired.
The last step involves specifying what should happen when the scheduled task is fired, which script you want to run. To do this, create a new action and browse the script you want to run.
Perform these steps for each script and remember to delay the last script (Delete Cloud Service) by 15 min according to deleting the Virtual machine to make sure that the cloud service has appeared in the portal before the script is fired. When finished you should have three different scheduled tasks up and running the only thing left is to wait and see if it works!
Hopefully you have learned something reading this article! I am aware of that it might be other ways to accomplish the same result, however this is the way I found solving this problem. Something that is worth adding is the opportunity to modify this a lot more, you could perform these steps manually declaring in detail how you want your virtual machines to be configured and deployed to Windows Azure, something that could be covered in another article in the future.