Last week I came across this post Scheduling Start and Stop Times for Azure VMs. So I was thinking, this should be possible with Amazon as well.
You know, running labs in the cloud is really awesome, but every minute costs money. My last two post Ultimate Guide to Citrix Lifecycle Management and Citrix Lifecycle Management Deep Dive did leverage Amazon AWS for testing.
Let me introduce you to AWS Tools for Windows PowerShell and show you how to Automatically Start and Stop Amazon VMs.
Download and install AWS Tools for Windows Powershell and run it as Administrator. The first time you run it, it will create an encrypted profile which contains your AWS Access Key, Secret Key and Default Location.
To make this all work I use the fantastic Devolutions Remote Desktop Manager. At the moment it doesn’t support Powershell as an event, so I leverage command line instead.
You’ll need to create a Start and Stop Powershell script for each Amazon VM with the correct Instance ID. It’s also possible to override the default location from the script.
1 2 3 |
Import-Module "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1" Set-DefaultAWSRegion -Region sa-east-1 Start-EC2Instance -Instance i-7ed1379c |
1 2 3 |
Import-Module "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1" Set-DefaultAWSRegion -Region sa-east-1 Stop-EC2Instance -Instance i-7ed1379c |
I’ve set the Post Execution Pause to 90 seconds which seems to be enough for the VM to start. Make sure to Run as Administrator.
1 |
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass D:\Start-GTW-AWS.ps1 |
1 |
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass D:\Stop-GTW-AWS.ps1 |
This is working really well. The money you’ll save will pay for the Devolutions Remote Desktop Manager license itself.
A great tip for Amazon VMs is to assign elastic IPs, if not the external IP will change every time the VM is shut down.
Note that the Elastic IP is free is the machine is running. If you shutdown the EC2 instance, a small fee of $0.005 is charged per Elastic IP address not used.